8 Replies - 18225 Views - Last Post: 28 March 2012 - 01:52 PM Rate Topic: -----

#1 mgcdrd   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 83
  • Joined: 22-November 09

how do srand and rand work?

Posted 28 March 2012 - 12:58 PM

I saw a pos t a few minutes ago about using rand () and srand(), and realized I do not how they actually work together, and the different seeds typically passed to them. For my searching, I got the passing a seed (explain that one too) to srand() sets up the way rand works, but I can't find anything I depth enough for my curiosity. I guess I'm saying, if calling srand only once, how can we modify things to create greater randomness in the numbers created?

Is This A Good Question/Topic? 0
  • +

Replies To: how do srand and rand work?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14417
  • View blog
  • Posts: 57,803
  • Joined: 12-June 08

Re: how do srand and rand work?

Posted 28 March 2012 - 01:17 PM

Quote

if calling srand only once, how can we modify things to create greater randomness in the numbers created?

Choose better seeds? Pick something from a naturally random occurring event?


http://www.cplusplus...y/cstdlib/rand/
http://www.cplusplus.../cstdlib/srand/
Was This Post Helpful? 0
  • +
  • -

#3 MasterOfBinary   User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 33
  • Joined: 02-March 12

Re: how do srand and rand work?

Posted 28 March 2012 - 01:19 PM

Random number generators use an algorithm to compute random numbers, but it always does it the same way. So if you always did srand(1), for example, instead of using the time, it would always produce the same set of numbers.

Since that isn't very helpful (or random) usually the generator is seeded using the time, which is random enough for most purposes.
Was This Post Helpful? 1
  • +
  • -

#4 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: how do srand and rand work?

Posted 28 March 2012 - 01:20 PM

You don't modify anything to get better seeds; you get what you get with those functions. All 'srand' does is set a seed value to what you pass to it and 'rand' modifies this so the next value you get isn't the same value. there is no defined behavior for what you will get after passing a value 'srand', its only purpose is to allow you to change how 'rand' works each time your program starts up.
Was This Post Helpful? 0
  • +
  • -

#5 mgcdrd   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 83
  • Joined: 22-November 09

Re: how do srand and rand work?

Posted 28 March 2012 - 01:21 PM

I looked at those and came up with far more question than answers. I am the type of person I have to completely understand things like this, so I ask.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14417
  • View blog
  • Posts: 57,803
  • Joined: 12-June 08

Re: how do srand and rand work?

Posted 28 March 2012 - 01:27 PM

Okay - then specify your questions. Right now it's becoming a guessing game of what you want to know versus what random factoids we can caught up! ;)
Was This Post Helpful? 0
  • +
  • -

#7 mgcdrd   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 83
  • Joined: 22-November 09

Re: how do srand and rand work?

Posted 28 March 2012 - 01:30 PM

So essentially you have to call srand prior to rand, which more or less sets the limits to which rand will create the numbers. And hence why you only call it once per program, and usually with time(0). And thisway it causes the greatest varience in the numbers generated.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14417
  • View blog
  • Posts: 57,803
  • Joined: 12-June 08

Re: how do srand and rand work?

Posted 28 March 2012 - 01:32 PM

@#7 - if that was a question I would agree with it.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg   User is offline

  • member icon

Reputation: 5568
  • View blog
  • Posts: 17,215
  • Joined: 25-December 09

Re: how do srand and rand work?

Posted 28 March 2012 - 01:52 PM

Quote

So essentially you have to call srand prior to rand

No you do no have to call srand prior to rand.

Quote

which more or less sets the limits to which rand will create the numbers

No calling srand() sets the starting point, not the limits. The maximum value that rand will return is defined as RAND_MAX.

Quote

And hence why you only call it once per program

You normally only call srand once because each call to srand changes the starting point of the random sequence to a new location. If you call srand with the same seed you will always be starting at the same location.

Quote

and usually with time(0)

You use time(0) to seed the generator with the seed value returned by the time() function which will usually produce a different starting point each time you call srand. As long as you wait long enough between calls so time(0) returns a different value.

So when you call rand before you call srand the starting point will be the same as if you called srand(1). If you call srand with the same seed then your first random number will be the same, and the sequence will be repeatable.

Jim

This post has been edited by jimblumberg: 28 March 2012 - 01:53 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1