I don't understand the meaning of srand() in <time.h> to create a random number.
Here is my code:
/* srand example */
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
int main ()
{
printf ("First number: %d\n", rand()%100);
srand (time(NULL));
printf ("Random number: %d\n", rand()%100);
srand (1);
printf ("Again the first number: %d\n", rand()%100);
return 0;
}
and the results are:
First number: 41
Random number: 13
Again the first number: 41
Why is the result of srand(1) different from srand(2)?
Why does the result of srand(1) or srand(2) keep appearing again and again?
And why do I have to use srand(time(NULL)) in order to create a dynamic random number?
If you look at the docs:
Seeds the pseudo-random number generator used by std::rand() with the value seed.
rand() has some internal state that it keeps from one call to the next. The function is deterministic - but we can view its output as pseudorandom. So the value produced by:
srand(1);
rand();
will always be the same for a given implementation. That's why the notes state that:
Generally speaking, the pseudo-random number generator should only be seeded once, before any calls to rand(), and the start of the program. It should not be repeatedly seeded, or reseeded every time you wish to generate a new batch of pseudo-random numbers.
srand() uses a seed to determine what the number will be. srand() always produces the same number when given the same seed. If you don't give it a seed, then it uses something in the system to determine what the seed will be, and this makes the numbers appear random - the seed is constantly changing. srand(1) will always be the same.
The function rand() (and most other “random”
sources1) is
an implementation of a “pseudo-random number generator”.
The numbers it generates are not random at all, but simply numbers in a
very long sequence of discreet values; the sequence is designed so that
successive numbers appear random, according to some suitable definition
of random.
The function srand() simply sets a starting point in this sequence.
[1] Most OS do have some source of truly random numbers, such as the
file /dev/random on Unix systems. They can be very slow for more than
a few bytes, however. There main use is to seed a PRNG.
Hmm, you need better understanding of how Pseudo-Random Number Generators (PRNGs) work. The word "Pseudo" is very important: actually it is very hard to generate really random number, it is easier to take it once (like time in seconds) then compute other values based on it.
I.e. Linear Congruential Generator which is often used for libc rand() calculates random number based on previous value, so:
First X is set by srand()
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 1
Seed values are integers that define the exact sequence of pseudo-random numbers, but there's no way of knowing ahead of time what sequence it will be and there's no way of tweaking a sequence by slightly changing the seed. Even the tiniest change in seed value will result in a radically different random sequence.
Related
My understanding is that a pseudo-random number generator basically just takes some number (the seed), hashes it with a bunch of XORs and bitshifts, and then spits out a really long number from which a remainder can be retrieved to get your "random" number.
Now, usually you'd use time(NULL) as the seed for rand() in C/C++. However, time(NULL) only increments every second, not every millisecond. So how, then, can I for loop over rand() a thousand times in less than one second and still get different numbers as outputs if the seed is still the same time(NULL) value?
rand() uses the previous random value as the new seed on subsequent calls. This is why a unique random sequence of values will be generated when you start with a different seed value.
A pseudo number generator outputs a deterministic series of numbers in a certain range that are supposed to look random.
The time(NULL) is the so called seed of the RNG and tells it, where in the series to start. You should only do this once per program.
By the way, rand() is not modern C++. See here for why and what to do instead.
It's common for C / C++ compilers to use a linear congruential generator for rand().
My goal is generate 2D or 3D geometry without having to store it on disk, so my goal is to have any sort of function than generate the same values according to a small seed. I don't mean to seek random values, but if the same "random" garbage data is returned when given the same seed, that's something I'm looking for.
If I give srand() the same integer, I get the same sequence out of rand(). Is that an intended feature? If not, are there known standard functions designed to do the same thing?
Although I tried this on ideone and on my computer and I get different results, I can understand that those function's implementations are not described, so that explains it.
If I give srand() the same integer, I get the same sequence out of rand(). Is that an intended feature?
Yes, see 7.20.2.2:
7.20.2.2 The srand function
[...] Description
The srand function uses the argument as a seed for a new sequence of pseudo-random
numbers to be returned by subsequent calls to rand. If srand is then called with the
same seed value, the sequence of pseudo-random numbers shall be repeated.
However, that's only true for the same implementation of srand/rand. Another implementation might not use the same algorithm, and therefor won't produce the same sequence.
If not, are there known standard functions designed to do the same thing ?
Well, the functions are standard, but only in their behaviors, not the actual values (see implementation remark above). You're better off by using a specific generator from the C++11 predefined random number generators, since they're standardized.
"If I give srand() the same integer, I get the same sequence out of
rand(). Is that an intended feature ?"
Yes.
If you seed the same random number generator with the same seed, it will produce the same result.
Standard library rand and all it's variants are usually implemented as Linear congruential generators. They are not truly random, and perhaps better referred to as psuedo-random.
You probably saw different results on different machines because either they were using different psuedo-random number generation algorithms or you weren't supplying a fixed seed in which case the current system time is often the default seed.
If you need a fixed set of psuedo-random data, then generate it once and store it.
The answer is yes, you get a repeatable sequence, if you always use the same implementation and the same seed, though it might be ill-advised due to possibly poor quality of rand().
Better use the C++ random number framework in <random> though. It not only allows reproducible sequences across implementations, it also supplies all you need to reliably get the distribution you really want.
Now to the details:
The requirements on rand are:
Generates pseudo-random numbers.
Range is 0 to RAND_MAX (minimum of 32767).
The seed set by srand() determines the sequence of pseudo-random numbers returned.
There is no requirement on what PRNG is implemented, so every implementation can have its own, though Linear Congrueantial Generators are a favorite.
A conforming (though arguably useless) implementation is presented in this dilbert strip:
http://dilbert.com/strips/comic/2001-10-25/
Or for those who like XKCD (It's a perfect drop-in for any C or C++ library ;-)):
For completeness, the standard quotes:
7.22.2.1 The rand function
The rand function computes a sequence of pseudo-random integers in the range 0 to
RAND_MAX.
[...]
The value of the RAND_MAX macro shall be at least 32767.
7.22.2.2 The srand function
The srand function uses the argument as a seed for a new sequence of pseudo-random
numbers to be returned by subsequent calls to rand. If srand is then called with the
same seed value, the sequence of pseudo-random numbers shall be repeated. If rand is
called before any calls to srand have been made, the same sequence shall be generated
as when srand is first called with a seed value of 1.
If you seed the random number generator with the same value, it will produce the same result. You saw different results on different machines because they were (probably) using different random number generation algorithms.
I have a C++11 program that needs to create several independent random generators, for use by different threads in a parallel computation. These generators should be initialized with different seed values so that they all produce different pseudo-random sequences.
I see that there's a std::seed_seq class that seems to be meant for this purpose, but it's not clear what's the right way to construct one. The examples I've seen, such as the one on cppreference.com, initialize it with a handful of integer constants hard-coded in the program:
std::seed_seq seq{1,2,3,4,5};
I doubt that's actually a recommended best practice, so I'm wondering what is the recommended practice. In particular:
Since a seed_seq can be initialized with an arbitrary number of integers, what's the significance of the length of its initializer list? If I want to produce seeds for 100 random generators, do I need to initialize my seed_seq with 100 integers?
If the length of the initializer list doesn't have to match the number of seeds I intend to generate, is it OK to initialize a seed_seq with just one integer and then use it to produce a large number of seeds?
How about initializing with no integers, i.e. using the default constructor? (This means I'd get the same seeds every time, of course.)
If it's OK to construct a seed_seq from a single integer and then generate lots of seeds from it, what's the benefit of using seed_seq instead of an ordinary random generator? Why not just construct a std::mt19937 from that single integer and use that to produce seed values for other generators?
The trouble with using a fixed sequence like that is that you get the same sequence of seeds out of it, much the same as if you had called srand(42) at the start of your program: it generates identical sequences.
The C++11 standard states (in section 26.5.7.1 Class seed_seq):
A seed sequence is an object that consumes a sequence of integer-valued data and produces a requested number of unsigned integer values i, 0 i < 232, based on the consumed data.
[Note: Such an object provides a mechanism to avoid replication of streams of random variates. This can be useful, for example, in applications requiring large numbers of random number engines. —end note]
It also states how those integers are turned into seeds in paragraph 8 of that section, in such a way that the distribution of those seeds is acceptable even if the integer input items are very similar. So you can probably think of it as a pseudo-random number generator for seed values.
A larger number of items will provide more "randomness" in the seed values, provided they have some randomness themselves. Using constants as input is a bad idea for this reason.
What I tend to do is very similar to the way you normally randomise one generator, with srand (time (0)). In other words:
#include <random>
#include <cstdint>
#include <ctime>
#include <iostream>
int main()
{
std::seed_seq seq{time(0)};
std::vector<std::uint32_t> seeds(10);
seq.generate(seeds.begin(), seeds.end());
for (std::uint32_t n : seeds) {
std::cout << n << '\n';
}
}
If you have multiple sources of randomness, such as a value read from /dev/random under Linux, or a white noise generator of some description, or the average number of milliseconds between keypresses the last time a user ran this program, you could use those as extra inputs:
std::seed_seq seq{time(0), valFromDevRandom(), getWhiteNoise(), avgMillis()};
but I doubt constants are the way to go, since they add no randomness to the equation.
According The C++11 standard (in section 26.5.7.1.8),seed_seq can generate a sequence which is likely generated by a hash function, uniformly and randomly in the range.
I try to answer the below questions:
Q1 "Since a seed_seq can be initialized with an arbitrary number of integers, what's the significance of the length of its initializer list? If I want to produce seeds for 100 random generators, do I need to initialize my seed_seq with 100 integers?"
A1. You needn't initialize seed_seq with a lot integers. Even seed_seq initialized by one random integer, the generated sequence keep the randomness. But you initialize seed_seq with more integers and in the wider range, The generated sequence is more hardly "collide" by attackers.
Q2. "If the length of the initializer list doesn't have to match the number of seeds I intend to generate, is it OK to initialize a seed_seq with just one integer and then use it to produce a large number of seeds?"
A2. Yes, it is OK to initialize a seed_seq with just one integer if you don't need cryptographically secure level.
Q3. "How about initializing with no integers, i.e. using the default constructor? (This means I'd get the same seeds every time, of course.)"
A3. You will get the identical sequences by the default constructed seed_seq runs more. Thus it will became a security hole.
Q4. "If it's OK to construct a seed_seq from a single integer and then generate lots of seeds from it, what's the benefit of using seed_seq instead of an ordinary random generator? Why not just construct a std::mt19937 from that single integer and use that to produce seed values for other generators?"
A4. seed_seq is a light-weight algorithm, only iterates the filled sequence 3 times. I guess you can use other random generator instead of seed_seq.
Simple question but difficult to me. I want to generate uniformly distributed random numbers between 0 and 1, how can I do it? In matlab I am using rand, in C++ rand() returns integers.
Since C++11, use std::uniform_real_distribution
maybe you can use the RAND_MAX constant to divide the randomly generated integer
It really depends on how random you need the numbers to be. Generally, if I don't care about how random it is I will just use rand and divide by MAX_RAND (I bet matlab's rand is better than c's rand).
However, most of what I have done has required a better random number generator than the c function rand, and I don't need it to be cryptographically secure. In this case I use a mersenne twister class (http://www.bedaux.net/mtrand/) which has seemed to work better for the simulated annealing and monte carlo simulations that I sometimes find myself doing.
Also with this class, there is a way to get a random int32 and a radom double (between 0 and 1) out.
At http://www.cplusplus.com/reference/clibrary/cstdlib/rand/ , I read the following : This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using srand.
What does seed mean and how does rand() use seed to generate the series?
rand() uses a so-called pseudo-random number generator. It generates not really random numbers but a deterministic sequence that appears to look random enough and satisfy some statistical properties. The seed is essentially the starting value of that sequence; given the same seed, the PRNG will always produce the same sequence. That is why you often seed with something that is not too deterministic, e.g. the current time (although that fails if you re-seed the PRNG in a tight loop or run the program fast enough in succession or parallel).
In most cases the PRNG in C is a simple linear congruential generator. It calculates the next number in a sequence with the following equation:
a and b here are values that have to be chosen with care to avoid horrible results. For example, for obvious reasons 2 is a very very bad choice for a. c just reduces the number to a certain range and is often a power of two. The seed simply supplies the 0th value.
Very crudely, it is something like:
int rand() {
return last_random_val =
((last_random_val * 1103515245) + 12345) & 0x7fffffff);
}
void srand(int seed) {
last_random_val = seed;
}
And the last_random_val is set to the seed when you call srand(). Hence, for the same seed, same sequence of numbers are generated.