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 support@ccsinfo.com

Rand() returns same values

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



Joined: 07 Nov 2008
Posts: 2

View user's profile Send private message

Rand() returns same values
PostPosted: Fri Nov 07, 2008 1:24 pm     Reply with quote

Hi, I do not have a good English, I occupy a translator.

I'm doing a game of dices, but the function Rand () always returns the same values, in C we can occupy srand (time ()) with time() but in PIC C is not the function time ().
I am using the PIC16F877A

In short I am doing this:
srand(7); //Should be srand(time());
randomNum= (rand()%6)+1; //range 1-6
randomNum2= (rand()%6)+1;
setDice(randomNum,1); //setDice function that I did
setDice(randomNum2,2);

I tried to do 1000 forms, but always returns the same order of values

Please help!!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 07, 2008 1:49 pm     Reply with quote

Quote:
srand(7);

It's not a random number generator. It's a pseudo random
number generator. The srand() function sets the initial state of the
generator. You are using the same seed (7) every time. So you
will get the same sequence every time. To change the sequence
you need to use a different seed.

These threads discuss the problem of how to generate the seed:
http://www.ccsinfo.com/forum/viewtopic.php?p=83928
http://www.ccsinfo.com/forum/viewtopic.php?t=27007
http://www.ccsinfo.com/forum/viewtopic.php?t=1313
Alvaro



Joined: 07 Nov 2008
Posts: 2

View user's profile Send private message

THANKS
PostPosted: Fri Nov 07, 2008 2:00 pm     Reply with quote

Thank you very much,
I took the link: http://www.ccsinfo.com/forum/viewtopic.php?t=1313
Works!!!
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