Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
2 registered members (AndrewAMD, TipmyPip), 12,420 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
rand() and srand(seed) #115178
03/05/07 13:13
03/05/07 13:13
Joined: Jul 2006
Posts: 783
London, UK
sheefo Offline OP
User
sheefo  Offline OP
User

Joined: Jul 2006
Posts: 783
London, UK
I do not understand the concept of srand. I am using "rand() % 100 + 1" to generate a random number in a range of 1 to 100. Each time I do so it generates the same "random" number, so I guess it is not random then.

Do I use srand to sort this out? What does srand do? I need an explination, not a direction to a C++ reference (they do me no good on this subject).

Re: rand() and srand(seed) [Re: sheefo] #115179
03/06/07 03:22
03/06/07 03:22
Joined: Mar 2007
Posts: 4
New York, NY
DGuy Offline
Guest
DGuy  Offline
Guest

Joined: Mar 2007
Posts: 4
New York, NY
srand() "seeds" the random number generator (i.e. rand()).

If you call srand(100), then call rand() 10 times, then call srand(100), then finally call rand() 10 more times, you'll get the same set of 10 random numbers both times.

The reason you are getting the same "random" numbers is probably because the "seed" number is always the same at program start.

To get different random number each run, try srand(GetTickCount())

GetTickCount() returns the number of millisecs since the computer started.

HTH

Re: rand() and srand(seed) [Re: DGuy] #115180
03/06/07 09:24
03/06/07 09:24
Joined: Jul 2006
Posts: 783
London, UK
sheefo Offline OP
User
sheefo  Offline OP
User

Joined: Jul 2006
Posts: 783
London, UK
Thank you. It works now. Your explanation was better than any C++ reference I found. I use http://www.cplusplus.com/

I always thought that "random" meant random in programming. Apparently there just the same sequence of numbers.

Re: rand() and srand(seed) [Re: sheefo] #115181
03/06/07 19:58
03/06/07 19:58
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
well, of the generator. you should also be cautious with utilizations like these:
rand()%40000, because RAND_MAX is often defined as 65535, so in this case you'll get the numbers between 0 and 25535 twice as often as the ones above this interval.


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1