View previous topic :: View next topic |
Author |
Message |
teekaytk
Joined: 14 Jan 2005 Posts: 28
|
rand() |
Posted: Thu May 11, 2006 4:17 am |
|
|
hi all
i want to generate a random number betwwen 0 and 3, how can i use rand() to generate this number, please help
thanks
TK |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu May 11, 2006 6:14 am |
|
|
Check the help file. It shows the usage as well as the srand() function.
Code: |
#define RAND_MAX 3
num = rand();
|
|
|
|
teekaytk
Joined: 14 Jan 2005 Posts: 28
|
|
Posted: Thu May 11, 2006 6:20 am |
|
|
thanks, it works |
|
|
Daniel T. Guest
|
How should I set the seed? |
Posted: Fri May 12, 2006 7:31 pm |
|
|
How should I go about setting the SEED? |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
|
Posted: Fri May 12, 2006 7:39 pm |
|
|
In the CCS manual...srand() |
|
|
Daniel T. Guest
|
|
Posted: Fri May 12, 2006 8:42 pm |
|
|
But if I hard code a value for the seed, the sequence will be the same each time the pic is powered on, correct? |
|
|
Daniel T. Guest
|
Random seeds for srand() |
Posted: Fri May 12, 2006 9:56 pm |
|
|
I think I'm going to use a trick PCs use. Most/some OSes read the RTC from hardware during boottime, and write that data back to hardware at shutdown. I'm going to store a seed number in eeprom. On power up this seed will be read from eeprom. The seed will be used, and every so often a new, randomly generated, seed will be stored back into eeprom. The new seed will be stored after human triggered events, giving truly random seeds for next time the pic is powered on. |
|
|
|