Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number generator - why seed every time

Tags:

c++

c

I am relative new to c and c++. In java, the language I am used to program in, its very easy to implement random number generation. Just call the the static random-method from a class called Math.

int face = ((int)(Math.random() * 6) + 1);

simulates a dice-throw ...

In c and c++ you have to "seed the random number generator" , by calling the srand-function

srand ( time(NULL) );

What is the point of doing this - I mean is there any advantage of having to seed the random number generator every time the code is run?

like image 455
user2991252 Avatar asked Dec 17 '13 14:12

user2991252


People also ask

Why do we use seed 42 randomly?

The number "42" was apparently chosen as a tribute to the "Hitch-hiker's Guide" books by Douglas Adams, as it was supposedly the answer to the great question of "Life, the universe, and everything" as calculated by a computer (named "Deep Thought") created specifically to solve it.

What is a seed in random number generation?

A random seed (or seed state, or just seed) is a number (or vector) used to initialize a pseudorandom number generator. For a seed to be used in a pseudorandom number generator, it does not need to be random.

How do seeds work in random?

How Seed Function Works ? Seed function is used to save the state of a random function, so that it can generate same random numbers on multiple executions of the code on the same machine or on different machines (for a specific seed value). The seed value is the previous value number generated by the generator.

How do I fix a random seed in Excel?

To stop the RAND or RANDBETWEEN functions from recalculating in one cell, select that cell, switch to the formula bar and press F9 to replace the formula with its value. To prevent an Excel random function from recalculating, use the Paste Special > Values feature.


4 Answers

Given the same seed, a pseudo random number generator will produce the same sequence every time. So it comes down to whether you want a different sequence of pseudo random numbers each time you run, or not.

It really depends on your needs. There are times when you want to repeat a sequence. And times when you do not. You need to understand the needs of each specific application.

One thing you must never do is seed repeatedly during generation of a single sequence. Doing so very likely will destroy the distribution of your sequence.

like image 73
David Heffernan Avatar answered Sep 28 '22 23:09

David Heffernan


What's usually called a random number generator is actually a pseudo-random number generator. This typically means that you can generate the same random sequence if you provide the "key" to that sequence, referred to as the "seed". This is very useful when you wish to test your algorithm that is based on randomization, and you need to ensure repeatable results.

If you do not "seed" your Random number generator, it is seeded with some (usually based on system time) random number by default, and therefore produces the different sequence every time that you run your program.

like image 28
Sergey Kalinichenko Avatar answered Sep 29 '22 01:09

Sergey Kalinichenko


If you don't seed the generator, it will have the same seed every time you run your program, and the random number sequence will be the same each time.

Also note that you only should to seed the generator once, at the beginning of the program.

like image 10
Some programmer dude Avatar answered Sep 29 '22 01:09

Some programmer dude


The advantage is that you can repeat a random number sequence by supplying the same seed.

The game Elite used this to store an entire world, consisting of thousands of stars, as a single number. To generate the exact same world a second time, the just supplied the same seed.

like image 8
Klas Lindbäck Avatar answered Sep 29 '22 00:09

Klas Lindbäck