Gamestudio Links
Zorro Links
Newest Posts
Newbie Questions
by AndrewAMD. 12/05/23 10:56
Zorro Trader GPT
by TipmyPip. 12/04/23 11:34
Square root rule
by Smallz. 12/02/23 09:15
RTest not found error
by TipmyPip. 12/01/23 21:43
neural function for Python to [Train]
by TipmyPip. 12/01/23 14:47
Xor Memory Problem.
by TipmyPip. 11/28/23 14:23
Training with command line parameters
by TipmyPip. 11/26/23 08:42
Combine USD & BTC Pairs In Asset Loop
by TipmyPip. 11/26/23 08:30
AUM Magazine
Latest Screens
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Who's Online Now
6 registered members (AndrewAMD, alibaba, fairtrader, ozgur, TipmyPip, Quad), 622 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
fairtrader, hus, Vurtis, Harry5, KelvinC
19019 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 | chip programmers | 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