1

Can someone explain to me what seed value is? For example I have this code:

int MIN=0;
int MAX=100;

srand((unsigned) time(NULL));

srand(2020);

int num = (int)(rand() / (RAND_MAX + 1.0 + MIN) * MAX);

I am required to use a seed value of 2020 to generate numbers between 0 to 100. I checked everywhere but seems like there is no tutorial that explains what seed value is, and how the code should change from the default seed value. Line 3 of my code assumes the default seed value, so it does not work with a seed value of 2020. Also, MAX is 100, and MIN is 0.

Thanks in advance!

Hidaisyen
  • 29
  • 4
  • The standard says nothing about how `rand()` should be implemented, but it's usually one of these: https://en.wikipedia.org/wiki/Linear_congruential_generator – Shawn Dec 12 '20 at 11:10
  • properly post your code – 0___________ Dec 12 '20 at 11:23
  • https://en.wikipedia.org/wiki/Random_seed – 0___________ Dec 12 '20 at 11:25
  • If you don't call `srand()` at all, then the default seed is used (whatever that is) and `rand()` probably produces the same sequence. If you call `srand(2020)` then that seed is used every time the program is run, and again, the same sequence is produced, but different from the first. If you call `srand(time(NULL))` the PRNG is seeded with the current time in seconds, so you get a different sequence each time the program is run (if one second has ticked). – Weather Vane Dec 12 '20 at 11:33
  • Aside from the seed issues, `rand() / (RAND_MAX + 1.0 + MIN) * MAX` is not a correct way to scale the range. For a simple class assignment, you can use `rand() % (MAX-MIN+1) + MIN`, assuming you want to include both `MAX` and `MIN`. This has some deficiencies when `rand` is low quality, but it is tolerable for schoolwork. – Eric Postpischil Dec 12 '20 at 13:25
  • [This answer](https://stackoverflow.com/a/48505649/2166798) to another question is not specific to C or `rand/srand`, but it explains conceptually how pseudo-random number generators work. In the case of lcg-based implementations `rand`, the `f()` and `h()` functions referred to are `f(seed) == seed` and `h(state) == state`. – pjs Dec 12 '20 at 19:36
  • @EricPostpischil Using modulo arithmetic to generate a range is subject to [modulo bias](https://stackoverflow.com/a/10984975/2166798), not recommended. – pjs Dec 12 '20 at 19:46
  • @pjs: My comment says it has some deficiencies but is tolerable for schoolwork. It is, at this level. – Eric Postpischil Dec 12 '20 at 19:52
  • @EricPostpischil I'm not a big fan of teaching students to do things the wrong way. – pjs Dec 12 '20 at 19:56
  • @pjs: Nor am I, but teaching a better method is too much for a comment in this situation. Their existing code was grossly wrong, so a quick fix was provided. If you want to provide a better solution, go for it. – Eric Postpischil Dec 12 '20 at 19:58

5 Answers5

0

The pseudo-random number generator is initialized using the argument passed as seed. For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand.

http://www.cplusplus.com/reference/cstdlib/srand/

Then calling rand() will return a pseudo-random number in the range of 0 to RAND_MAX.

If you want rand() to produce different random numbers, ensure you are not calling srand(2020) each time you are calling rand(). As you only need to seed the random number generation once.

0RR
  • 1,538
  • 10
  • 21
0

The seed has 2 usages. The most common one is to start a random generator with a different value. It matters when you want to produce a random value for a crypto algorithm. When you start the program, you do not want to use the same value on each run...

The other usage is to generate a reproducible pseudo-random sequence. It matters when you want reproducible tests, or when you want to exchange with others with random data but want all to use the same sequence. It is very often used in tutorials to have a consistent sequence that the user will be able to reproduce when following the tuto. It is also used by teachers because it is easier to see if students have found the correct result (if every student uses a different sequence, all results would be different)

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

In C, srand takes a seed that determines the sequence of numbers rand will generate (also known as pseudorandom numbers), as long as the program doesn't change and the program uses the same implementation of rand and srand.

In C, srand and rand can't be used to create a reproducible sequence of pseudorandom numbers, because the standard that defines the C language doesn't specify what that sequence is, even if the seed is given. Notably, rand uses an unspecified pseudorandom number algorithm, and that algorithm can differ between C implementations, including versions of the same standard library. It's not the "compiler", the "system", or the "architecture" that decides which implementation of rand and srand is used. See also these questions:

Peter O.
  • 32,158
  • 14
  • 82
  • 96
0

The rand function generates some sequence of numbers. (The sequence is calculated but is intended to serve as if the numbers were random, so they are called pseudo-random.) By itself, it will always generate the same sequence of numbers in a program. We use srand to choose which sequence of numbers it generates. (Most commonly, rand generates a cycle of the same numbers, if called enough times, and srand merely chooses where in that cycle we start.)

So, you call srand to set the starting point of the sequence for rand. The value you pass to srand is any unsigned int value. In the C standard, there is no specified meaning for the value. You just give srand a number like 1 to get one sequence, 2 to get a different sequence, 3 to get another sequence, and so on. You have no other control over the actual values generated; setting the seed to some particular value does not guarantee the first rand call will return any particular documented value.

The C standard also does not specify any method for calculating the numbers in the sequence. Each C implementation may choose its own method. Some C implementations use bad methods, in which the numbers are not very random-like at all, and some patterns can be easily observed. (For this reason, it is often recommended to use alternatives, like Unix’s srandom and random.)

When a program is being debugged or is being used as a class assignment, it is common to call srand with a fixed value, like srand(2020). This results in rand generating the same numbers each time. That makes it easier to debug the program or to check the results of student programs. When varying sequences of numbers are designed, it is common to call srand with srand(time(NULL)). Assuming the time is available (time(NULL) may return −1 for an error), this causes the program to use different sequences at different times. (Commonly, the value of time as an integer changes once per second, but this can vary depending on the C implementation.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
-2

To generate a random number with a specific seed use srand to set the seed for one-time only.
Then use rand to generate the random number. The default range is between 0 and RAND_MAX (RAND_MAX = 32767).
To define the maximum number just override RAND_MAX via precompiler statements (Side effects included) or use Modulo %.

Override RAND_MAX

#ifdef RAND_MAX
#undef RAND_MAX
#define RAND_MAX 100
#endif

Use Modulo

srand(2020);
int random_number = rand() % 100;