CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

rand()

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
teekaytk



Joined: 14 Jan 2005
Posts: 28

View user's profile Send private message

rand()
PostPosted: Thu May 11, 2006 4:17 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu May 11, 2006 6:14 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu May 11, 2006 6:20 am     Reply with quote

thanks, it works
Daniel T.
Guest







How should I set the seed?
PostPosted: Fri May 12, 2006 7:31 pm     Reply with quote

How should I go about setting the SEED?
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri May 12, 2006 7:39 pm     Reply with quote

In the CCS manual...srand()
Daniel T.
Guest







PostPosted: Fri May 12, 2006 8:42 pm     Reply with quote

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()
PostPosted: Fri May 12, 2006 9:56 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group