View previous topic :: View next topic |
Author |
Message |
picj1984
Joined: 01 Mar 2010 Posts: 73
|
rand(), Need to change RAND_MAX definition during program |
Posted: Tue Jul 13, 2010 2:28 pm |
|
|
First I want to apologize for not being able to find this using the search machine. Seems like a fairly common request (changing definitions during runtime) I found a few things but it still doesn't make sense to me.
Long story short, I'm using the rand() function successfully. The only problem is, I want to be able to change the RAND_MAX definition during the program, but it doesn't seem to be working. I was trying something with the function below, but it is not working. I'm semi-aware that definitions are not supposed to be used like this during run-time, but I thought I'd give it a shot. The compiler didn't seem to mind. Any suggestions?
Code: |
void check_seqs() {
if(an0 <= 32)
{
#undef RAND_MAX
#define RAND_MAX 1
set_seq = 0;
led_floater();
output_low(PIN_B6);
}
else if(an0 > 32 && an0 <= 64)
{
#undef RAND_MAX
#define RAND_MAX 2
set_seq = 1;
led_floater();
output_low(PIN_A7);
}
else if(an0 > 64 && an0 <= 96)
{
#undef RAND_MAX
#define RAND_MAX 3
set_seq = 2;
led_floater();
output_low(PIN_A6);
}
else if(an0 > 96 && an0 <= 128)
{
#undef RAND_MAX
#define RAND_MAX 4
set_seq = 3;
led_floater();
output_low(PIN_C0);
}
else if(an0 > 128 && an0 <= 160)
{
#undef RAND_MAX
#define RAND_MAX 5
set_seq = 4;
led_floater();
output_low(PIN_C1);
}
else if(an0 > 160 && an0 <= 192)
{
#undef RAND_MAX
#define RAND_MAX 6
set_seq = 5;
led_floater();
output_low(PIN_C3);
}
else if(an0 > 192 && an0 <= 224)
{
#undef RAND_MAX
#define RAND_MAX 7
set_seq = 6;
led_floater();
output_low(PIN_C4);
}
else if(an0 > 224)
{
#undef RAND_MAX
#define RAND_MAX 8
set_seq = 7;
led_floater();
output_low(PIN_C5);
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jul 14, 2010 12:23 pm |
|
|
The source code for the CCS rand() routine is given in stdlib.h.
Look for the following text. The code is a short little routine that
occurs just after this line:
Quote: |
The random number implementation
|
Copy the source to your program and rename the function slightly,
so it doesn't conflict with "rand()". Then edit your new function so
RAND_MAX is a variable in the function's parameter list, instead of being
a global constant. In other words, modify the function to suit your needs. |
|
|
picj1984
Joined: 01 Mar 2010 Posts: 73
|
|
Posted: Thu Jul 15, 2010 1:49 pm |
|
|
PCM programmer,
You are brilliant. Can't believe I didn't think of that. Thanks!! |
|
|
|