View previous topic :: View next topic |
Author |
Message |
valemike Guest
|
Changing a the stdlib.h file... |
Posted: Thu Aug 26, 2004 7:57 am |
|
|
Currently, the rand() function uses stlib.h, and RAND_MAX is defined as:
[quote]
#ifndef RAND_MAX
#define RAND_MAX 32767 // The value of which is the maximum value
// ... returned by the rand function
#endif
[/endif]
Suppose I want only a random number between 0 and 16. May I change RAND_MAX to #define RAND_MAX 16
Or should I refrain from touching any CCS header files and just do it the conventional way of taking a modulo-16?
i guess both ways work, but i'm not sure if anything else in CCS's suite uses this variable internally.
-Mike |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Aug 26, 2004 8:17 am |
|
|
I believe there is a function call srand() that will help generate a random number with the seed that you decide on, for example:
int i;
srand(16);// sets the new seed value
i = rand();// random number between 0 and 16
Ronald |
|
|
valemike Guest
|
|
Posted: Thu Aug 26, 2004 9:28 am |
|
|
Oh cool. If that is the case, then I just need to seed it with a max value of 16 using srand(). Initially, I thought that the argument in srand() had nothing to do with the maximum number i get with rand().
Only one way to find out - try it... |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Aug 26, 2004 9:57 am |
|
|
I have never seen the random number generator seed used as a control for the span of the output. Please try it and tell us if it works. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
MikeValencia
Joined: 04 Aug 2004 Posts: 238 Location: Chicago
|
Doesn't work |
Posted: Thu Aug 26, 2004 10:50 am |
|
|
I tried it. Wrote the following code:
Code: |
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000) /* one instruction=1us? */
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <stdlib.h>
void main(void)
{
unsigned long number;
int i;
srand(16);
for (i=0; i<10; i++)
{
number = rand();
printf ("%ld\r\n", number);
}
}
|
I ran it, and printed out the results on hyperterminal:
Code: |
7269
32262
12000
14030
18368
32741
29187
19196
12028
4768
|
Looks like I have to do a modulo.
-Mike
"valemike" |
|
|
sh1
Joined: 04 Mar 2005 Posts: 5
|
|
Posted: Mon Jun 13, 2005 6:30 am |
|
|
You should do the following
Code: | #include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000) /* one instruction=1us? */
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#define RAND_MAX 16
#include <STDLIB.H>
void main(void)
{
unsigned long number;
int i;
srand(get_rtcc()); // this will create a new random seed based on the current time counter (wich is pretty random per se)
for (i=0; i<10; i++)
{
number = rand();
printf ("%ld\r\n", number);
}
} |
|
|
|
valemike Guest
|
|
Posted: Mon Jun 13, 2005 11:27 am |
|
|
Thanks. I'm done with that project and am no longer at the place i consulted at. Can't put the code in now.
Anyhow, thanks for that info. I should've looked closely at the stlib.h file last year and found it was so easy to change RAND_MAX and the #ifndef prevents it from being re-defined.
-Mike |
|
|
|