Why do I keep getting the same first number while I've seeded a generator with time?
Clash Royale CLAN TAG#URR8PPP
up vote
13
down vote
favorite
I don't understand why I keep getting the same 1st number when I've already seeded a default_random_engine
with time(0)
(C++ Primer tell me to usetime(0)
). Is it a problem of my computer? (Ubuntu, C++11)
I tried on a online compiler and it's interesting that I got the same 1st number using gcc
while not using clang++
.
https://wandbox.org/permlink/kiUg1BW1RkDL8y8c
Code:
#include <iostream>
#include <ctime>
#include <random>
using namespace std;
int main()
auto t = time(0);
cout << "time: " << t << endl;
default_random_engine e(t);
uniform_int_distribution<int> uniform_dist(0, 9);
cout << "sequence:";
for(int i = 0; i < 10; i++)
cout << uniform_dist(e);
cout << endl;
return 0;
Result:
As you can see I keep getting 6
as the first random number, no matter I use clang++
or g++
to compile.
c++
 |Â
show 10 more comments
up vote
13
down vote
favorite
I don't understand why I keep getting the same 1st number when I've already seeded a default_random_engine
with time(0)
(C++ Primer tell me to usetime(0)
). Is it a problem of my computer? (Ubuntu, C++11)
I tried on a online compiler and it's interesting that I got the same 1st number using gcc
while not using clang++
.
https://wandbox.org/permlink/kiUg1BW1RkDL8y8c
Code:
#include <iostream>
#include <ctime>
#include <random>
using namespace std;
int main()
auto t = time(0);
cout << "time: " << t << endl;
default_random_engine e(t);
uniform_int_distribution<int> uniform_dist(0, 9);
cout << "sequence:";
for(int i = 0; i < 10; i++)
cout << uniform_dist(e);
cout << endl;
return 0;
Result:
As you can see I keep getting 6
as the first random number, no matter I use clang++
or g++
to compile.
c++
4
That's one of the problems with random number generators, especially with such a limited set of numbers to choose from. You could probably improve it by using a better engine (likemt19937
). I also suggest you see the example in thisuniform_int_distribution
reference.
â Some programmer dude
54 mins ago
1
@Someprogrammerdude Hmm. I tried 2 combinations. 1. Enlarge(0, 9)
to(0, 10000)
don't change anything. 2. Replacetime(0)
withrandom_device
, it works. Interesting.
â Rick
41 mins ago
3
Usingtime(0)
the seed values aren't that different, unlike usingstd::random_device
. If you still wanted to usetime(0)
you could calle.discard(n);
wheren
is the number of steps you want to advance the generator's state. If effect, throwing away the initialn
values.
â Blastfurnace
38 mins ago
1
Ifstd::default_random_engine
is implemented asrand
, thentime(0)
is not going to produce vastly different seeds which will lead to a random sequence that is close the the last one. Stay away fromstd::default_random_engine
as you don't know what it is. Unless you really need performance I would default to using astd::mt19937
as your go to PRNG
â NathanOliver
35 mins ago
2
It really depends on the generator. It's conceivable that the most-significant bits of the seed have a greater effect on the generator's initial state. My advice, use a better seed likestd::random_device
and maybe usediscard()
to "warm up" the generator before use.
â Blastfurnace
19 mins ago
 |Â
show 10 more comments
up vote
13
down vote
favorite
up vote
13
down vote
favorite
I don't understand why I keep getting the same 1st number when I've already seeded a default_random_engine
with time(0)
(C++ Primer tell me to usetime(0)
). Is it a problem of my computer? (Ubuntu, C++11)
I tried on a online compiler and it's interesting that I got the same 1st number using gcc
while not using clang++
.
https://wandbox.org/permlink/kiUg1BW1RkDL8y8c
Code:
#include <iostream>
#include <ctime>
#include <random>
using namespace std;
int main()
auto t = time(0);
cout << "time: " << t << endl;
default_random_engine e(t);
uniform_int_distribution<int> uniform_dist(0, 9);
cout << "sequence:";
for(int i = 0; i < 10; i++)
cout << uniform_dist(e);
cout << endl;
return 0;
Result:
As you can see I keep getting 6
as the first random number, no matter I use clang++
or g++
to compile.
c++
I don't understand why I keep getting the same 1st number when I've already seeded a default_random_engine
with time(0)
(C++ Primer tell me to usetime(0)
). Is it a problem of my computer? (Ubuntu, C++11)
I tried on a online compiler and it's interesting that I got the same 1st number using gcc
while not using clang++
.
https://wandbox.org/permlink/kiUg1BW1RkDL8y8c
Code:
#include <iostream>
#include <ctime>
#include <random>
using namespace std;
int main()
auto t = time(0);
cout << "time: " << t << endl;
default_random_engine e(t);
uniform_int_distribution<int> uniform_dist(0, 9);
cout << "sequence:";
for(int i = 0; i < 10; i++)
cout << uniform_dist(e);
cout << endl;
return 0;
Result:
As you can see I keep getting 6
as the first random number, no matter I use clang++
or g++
to compile.
c++
c++
edited 9 mins ago
Yakk - Adam Nevraumont
175k19179360
175k19179360
asked 1 hour ago
Rick
1,589825
1,589825
4
That's one of the problems with random number generators, especially with such a limited set of numbers to choose from. You could probably improve it by using a better engine (likemt19937
). I also suggest you see the example in thisuniform_int_distribution
reference.
â Some programmer dude
54 mins ago
1
@Someprogrammerdude Hmm. I tried 2 combinations. 1. Enlarge(0, 9)
to(0, 10000)
don't change anything. 2. Replacetime(0)
withrandom_device
, it works. Interesting.
â Rick
41 mins ago
3
Usingtime(0)
the seed values aren't that different, unlike usingstd::random_device
. If you still wanted to usetime(0)
you could calle.discard(n);
wheren
is the number of steps you want to advance the generator's state. If effect, throwing away the initialn
values.
â Blastfurnace
38 mins ago
1
Ifstd::default_random_engine
is implemented asrand
, thentime(0)
is not going to produce vastly different seeds which will lead to a random sequence that is close the the last one. Stay away fromstd::default_random_engine
as you don't know what it is. Unless you really need performance I would default to using astd::mt19937
as your go to PRNG
â NathanOliver
35 mins ago
2
It really depends on the generator. It's conceivable that the most-significant bits of the seed have a greater effect on the generator's initial state. My advice, use a better seed likestd::random_device
and maybe usediscard()
to "warm up" the generator before use.
â Blastfurnace
19 mins ago
 |Â
show 10 more comments
4
That's one of the problems with random number generators, especially with such a limited set of numbers to choose from. You could probably improve it by using a better engine (likemt19937
). I also suggest you see the example in thisuniform_int_distribution
reference.
â Some programmer dude
54 mins ago
1
@Someprogrammerdude Hmm. I tried 2 combinations. 1. Enlarge(0, 9)
to(0, 10000)
don't change anything. 2. Replacetime(0)
withrandom_device
, it works. Interesting.
â Rick
41 mins ago
3
Usingtime(0)
the seed values aren't that different, unlike usingstd::random_device
. If you still wanted to usetime(0)
you could calle.discard(n);
wheren
is the number of steps you want to advance the generator's state. If effect, throwing away the initialn
values.
â Blastfurnace
38 mins ago
1
Ifstd::default_random_engine
is implemented asrand
, thentime(0)
is not going to produce vastly different seeds which will lead to a random sequence that is close the the last one. Stay away fromstd::default_random_engine
as you don't know what it is. Unless you really need performance I would default to using astd::mt19937
as your go to PRNG
â NathanOliver
35 mins ago
2
It really depends on the generator. It's conceivable that the most-significant bits of the seed have a greater effect on the generator's initial state. My advice, use a better seed likestd::random_device
and maybe usediscard()
to "warm up" the generator before use.
â Blastfurnace
19 mins ago
4
4
That's one of the problems with random number generators, especially with such a limited set of numbers to choose from. You could probably improve it by using a better engine (like
mt19937
). I also suggest you see the example in this uniform_int_distribution
reference.â Some programmer dude
54 mins ago
That's one of the problems with random number generators, especially with such a limited set of numbers to choose from. You could probably improve it by using a better engine (like
mt19937
). I also suggest you see the example in this uniform_int_distribution
reference.â Some programmer dude
54 mins ago
1
1
@Someprogrammerdude Hmm. I tried 2 combinations. 1. Enlarge
(0, 9)
to (0, 10000)
don't change anything. 2. Replace time(0)
with random_device
, it works. Interesting.â Rick
41 mins ago
@Someprogrammerdude Hmm. I tried 2 combinations. 1. Enlarge
(0, 9)
to (0, 10000)
don't change anything. 2. Replace time(0)
with random_device
, it works. Interesting.â Rick
41 mins ago
3
3
Using
time(0)
the seed values aren't that different, unlike using std::random_device
. If you still wanted to use time(0)
you could call e.discard(n);
where n
is the number of steps you want to advance the generator's state. If effect, throwing away the initial n
values.â Blastfurnace
38 mins ago
Using
time(0)
the seed values aren't that different, unlike using std::random_device
. If you still wanted to use time(0)
you could call e.discard(n);
where n
is the number of steps you want to advance the generator's state. If effect, throwing away the initial n
values.â Blastfurnace
38 mins ago
1
1
If
std::default_random_engine
is implemented as rand
, then time(0)
is not going to produce vastly different seeds which will lead to a random sequence that is close the the last one. Stay away from std::default_random_engine
as you don't know what it is. Unless you really need performance I would default to using a std::mt19937
as your go to PRNGâ NathanOliver
35 mins ago
If
std::default_random_engine
is implemented as rand
, then time(0)
is not going to produce vastly different seeds which will lead to a random sequence that is close the the last one. Stay away from std::default_random_engine
as you don't know what it is. Unless you really need performance I would default to using a std::mt19937
as your go to PRNGâ NathanOliver
35 mins ago
2
2
It really depends on the generator. It's conceivable that the most-significant bits of the seed have a greater effect on the generator's initial state. My advice, use a better seed like
std::random_device
and maybe use discard()
to "warm up" the generator before use.â Blastfurnace
19 mins ago
It really depends on the generator. It's conceivable that the most-significant bits of the seed have a greater effect on the generator's initial state. My advice, use a better seed like
std::random_device
and maybe use discard()
to "warm up" the generator before use.â Blastfurnace
19 mins ago
 |Â
show 10 more comments
1 Answer
1
active
oldest
votes
up vote
9
down vote
accepted
You are setting initial states into your random-generator that are very similar. Depending on the quality of the generator, this may or may not result in similar outputs. To illustrate, I've augmented your sample to (a) print only the first sequence, since that is what we care about, and (b) print several results of various resolution:
int main()
auto t = time(0);
cout << "time: " << t << endl;
default_random_engine e(t);
default_random_engine e2(t);
default_random_engine e3(t);
default_random_engine e4(t);
uniform_int_distribution<int> uniform_dist(0, 9);
uniform_int_distribution<int> uniform_dist2(0,999);
uniform_int_distribution<int> uniform_dist3(0,99999);
uniform_int_distribution<int> uniform_dist4(0,9999999);
cout << "sequence: ";
cout << uniform_dist(e) << " " << uniform_dist2(e2) << " " << uniform_dist3(e3) << " " << uniform_dist4(e4);
cout << endl;
return 0;
When run:
$ ./a.out
time: 1541162210
sequence: 7 704 70457 7070079
$ ./a.out
time: 1541162211
sequence: 7 704 70457 7070157
$ ./a.out
time: 1541162212
sequence: 7 704 70458 7070236
$ ./a.out
time: 1541162213
sequence: 7 704 70459 7070315
$ ./a.out
time: 1541162214
sequence: 7 704 70460 7070393
$ ./a.out
time: 1541162215
sequence: 7 704 70461 7070472
$ ./a.out
time: 1541162216
sequence: 7 704 70461 7070550
$ ./a.out
time: 1541162217
sequence: 7 704 70462 7070629
$ ./a.out
time: 1541162218
sequence: 7 704 70463 7070707
$ ./a.out
time: 1541162219
sequence: 7 704 70464 7070786
While I do not know exactly what this random-generator implementation is doing, you can easily see that it is performing a very simple transformation of your seed into state, and state into output values. As other comments have suggested, there are better random generators and better seeds. Also note that the quality varies between implementations; Visual Studio 2017 does not exhibit this behavior.
Dude, this experiment is so smart and straightforward. Very comprehensive, thanks.
â Rick
11 mins ago
As a slight improvement, time(0) could be fed into std::mt19937 (EDIT: I now see that pretty much everyone else has already recommended this).
â jwimberley
2 mins ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
You are setting initial states into your random-generator that are very similar. Depending on the quality of the generator, this may or may not result in similar outputs. To illustrate, I've augmented your sample to (a) print only the first sequence, since that is what we care about, and (b) print several results of various resolution:
int main()
auto t = time(0);
cout << "time: " << t << endl;
default_random_engine e(t);
default_random_engine e2(t);
default_random_engine e3(t);
default_random_engine e4(t);
uniform_int_distribution<int> uniform_dist(0, 9);
uniform_int_distribution<int> uniform_dist2(0,999);
uniform_int_distribution<int> uniform_dist3(0,99999);
uniform_int_distribution<int> uniform_dist4(0,9999999);
cout << "sequence: ";
cout << uniform_dist(e) << " " << uniform_dist2(e2) << " " << uniform_dist3(e3) << " " << uniform_dist4(e4);
cout << endl;
return 0;
When run:
$ ./a.out
time: 1541162210
sequence: 7 704 70457 7070079
$ ./a.out
time: 1541162211
sequence: 7 704 70457 7070157
$ ./a.out
time: 1541162212
sequence: 7 704 70458 7070236
$ ./a.out
time: 1541162213
sequence: 7 704 70459 7070315
$ ./a.out
time: 1541162214
sequence: 7 704 70460 7070393
$ ./a.out
time: 1541162215
sequence: 7 704 70461 7070472
$ ./a.out
time: 1541162216
sequence: 7 704 70461 7070550
$ ./a.out
time: 1541162217
sequence: 7 704 70462 7070629
$ ./a.out
time: 1541162218
sequence: 7 704 70463 7070707
$ ./a.out
time: 1541162219
sequence: 7 704 70464 7070786
While I do not know exactly what this random-generator implementation is doing, you can easily see that it is performing a very simple transformation of your seed into state, and state into output values. As other comments have suggested, there are better random generators and better seeds. Also note that the quality varies between implementations; Visual Studio 2017 does not exhibit this behavior.
Dude, this experiment is so smart and straightforward. Very comprehensive, thanks.
â Rick
11 mins ago
As a slight improvement, time(0) could be fed into std::mt19937 (EDIT: I now see that pretty much everyone else has already recommended this).
â jwimberley
2 mins ago
add a comment |Â
up vote
9
down vote
accepted
You are setting initial states into your random-generator that are very similar. Depending on the quality of the generator, this may or may not result in similar outputs. To illustrate, I've augmented your sample to (a) print only the first sequence, since that is what we care about, and (b) print several results of various resolution:
int main()
auto t = time(0);
cout << "time: " << t << endl;
default_random_engine e(t);
default_random_engine e2(t);
default_random_engine e3(t);
default_random_engine e4(t);
uniform_int_distribution<int> uniform_dist(0, 9);
uniform_int_distribution<int> uniform_dist2(0,999);
uniform_int_distribution<int> uniform_dist3(0,99999);
uniform_int_distribution<int> uniform_dist4(0,9999999);
cout << "sequence: ";
cout << uniform_dist(e) << " " << uniform_dist2(e2) << " " << uniform_dist3(e3) << " " << uniform_dist4(e4);
cout << endl;
return 0;
When run:
$ ./a.out
time: 1541162210
sequence: 7 704 70457 7070079
$ ./a.out
time: 1541162211
sequence: 7 704 70457 7070157
$ ./a.out
time: 1541162212
sequence: 7 704 70458 7070236
$ ./a.out
time: 1541162213
sequence: 7 704 70459 7070315
$ ./a.out
time: 1541162214
sequence: 7 704 70460 7070393
$ ./a.out
time: 1541162215
sequence: 7 704 70461 7070472
$ ./a.out
time: 1541162216
sequence: 7 704 70461 7070550
$ ./a.out
time: 1541162217
sequence: 7 704 70462 7070629
$ ./a.out
time: 1541162218
sequence: 7 704 70463 7070707
$ ./a.out
time: 1541162219
sequence: 7 704 70464 7070786
While I do not know exactly what this random-generator implementation is doing, you can easily see that it is performing a very simple transformation of your seed into state, and state into output values. As other comments have suggested, there are better random generators and better seeds. Also note that the quality varies between implementations; Visual Studio 2017 does not exhibit this behavior.
Dude, this experiment is so smart and straightforward. Very comprehensive, thanks.
â Rick
11 mins ago
As a slight improvement, time(0) could be fed into std::mt19937 (EDIT: I now see that pretty much everyone else has already recommended this).
â jwimberley
2 mins ago
add a comment |Â
up vote
9
down vote
accepted
up vote
9
down vote
accepted
You are setting initial states into your random-generator that are very similar. Depending on the quality of the generator, this may or may not result in similar outputs. To illustrate, I've augmented your sample to (a) print only the first sequence, since that is what we care about, and (b) print several results of various resolution:
int main()
auto t = time(0);
cout << "time: " << t << endl;
default_random_engine e(t);
default_random_engine e2(t);
default_random_engine e3(t);
default_random_engine e4(t);
uniform_int_distribution<int> uniform_dist(0, 9);
uniform_int_distribution<int> uniform_dist2(0,999);
uniform_int_distribution<int> uniform_dist3(0,99999);
uniform_int_distribution<int> uniform_dist4(0,9999999);
cout << "sequence: ";
cout << uniform_dist(e) << " " << uniform_dist2(e2) << " " << uniform_dist3(e3) << " " << uniform_dist4(e4);
cout << endl;
return 0;
When run:
$ ./a.out
time: 1541162210
sequence: 7 704 70457 7070079
$ ./a.out
time: 1541162211
sequence: 7 704 70457 7070157
$ ./a.out
time: 1541162212
sequence: 7 704 70458 7070236
$ ./a.out
time: 1541162213
sequence: 7 704 70459 7070315
$ ./a.out
time: 1541162214
sequence: 7 704 70460 7070393
$ ./a.out
time: 1541162215
sequence: 7 704 70461 7070472
$ ./a.out
time: 1541162216
sequence: 7 704 70461 7070550
$ ./a.out
time: 1541162217
sequence: 7 704 70462 7070629
$ ./a.out
time: 1541162218
sequence: 7 704 70463 7070707
$ ./a.out
time: 1541162219
sequence: 7 704 70464 7070786
While I do not know exactly what this random-generator implementation is doing, you can easily see that it is performing a very simple transformation of your seed into state, and state into output values. As other comments have suggested, there are better random generators and better seeds. Also note that the quality varies between implementations; Visual Studio 2017 does not exhibit this behavior.
You are setting initial states into your random-generator that are very similar. Depending on the quality of the generator, this may or may not result in similar outputs. To illustrate, I've augmented your sample to (a) print only the first sequence, since that is what we care about, and (b) print several results of various resolution:
int main()
auto t = time(0);
cout << "time: " << t << endl;
default_random_engine e(t);
default_random_engine e2(t);
default_random_engine e3(t);
default_random_engine e4(t);
uniform_int_distribution<int> uniform_dist(0, 9);
uniform_int_distribution<int> uniform_dist2(0,999);
uniform_int_distribution<int> uniform_dist3(0,99999);
uniform_int_distribution<int> uniform_dist4(0,9999999);
cout << "sequence: ";
cout << uniform_dist(e) << " " << uniform_dist2(e2) << " " << uniform_dist3(e3) << " " << uniform_dist4(e4);
cout << endl;
return 0;
When run:
$ ./a.out
time: 1541162210
sequence: 7 704 70457 7070079
$ ./a.out
time: 1541162211
sequence: 7 704 70457 7070157
$ ./a.out
time: 1541162212
sequence: 7 704 70458 7070236
$ ./a.out
time: 1541162213
sequence: 7 704 70459 7070315
$ ./a.out
time: 1541162214
sequence: 7 704 70460 7070393
$ ./a.out
time: 1541162215
sequence: 7 704 70461 7070472
$ ./a.out
time: 1541162216
sequence: 7 704 70461 7070550
$ ./a.out
time: 1541162217
sequence: 7 704 70462 7070629
$ ./a.out
time: 1541162218
sequence: 7 704 70463 7070707
$ ./a.out
time: 1541162219
sequence: 7 704 70464 7070786
While I do not know exactly what this random-generator implementation is doing, you can easily see that it is performing a very simple transformation of your seed into state, and state into output values. As other comments have suggested, there are better random generators and better seeds. Also note that the quality varies between implementations; Visual Studio 2017 does not exhibit this behavior.
answered 26 mins ago
Wheezil
1,6191826
1,6191826
Dude, this experiment is so smart and straightforward. Very comprehensive, thanks.
â Rick
11 mins ago
As a slight improvement, time(0) could be fed into std::mt19937 (EDIT: I now see that pretty much everyone else has already recommended this).
â jwimberley
2 mins ago
add a comment |Â
Dude, this experiment is so smart and straightforward. Very comprehensive, thanks.
â Rick
11 mins ago
As a slight improvement, time(0) could be fed into std::mt19937 (EDIT: I now see that pretty much everyone else has already recommended this).
â jwimberley
2 mins ago
Dude, this experiment is so smart and straightforward. Very comprehensive, thanks.
â Rick
11 mins ago
Dude, this experiment is so smart and straightforward. Very comprehensive, thanks.
â Rick
11 mins ago
As a slight improvement, time(0) could be fed into std::mt19937 (EDIT: I now see that pretty much everyone else has already recommended this).
â jwimberley
2 mins ago
As a slight improvement, time(0) could be fed into std::mt19937 (EDIT: I now see that pretty much everyone else has already recommended this).
â jwimberley
2 mins ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53118346%2fwhy-do-i-keep-getting-the-same-first-number-while-ive-seeded-a-generator-with-t%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
4
That's one of the problems with random number generators, especially with such a limited set of numbers to choose from. You could probably improve it by using a better engine (like
mt19937
). I also suggest you see the example in thisuniform_int_distribution
reference.â Some programmer dude
54 mins ago
1
@Someprogrammerdude Hmm. I tried 2 combinations. 1. Enlarge
(0, 9)
to(0, 10000)
don't change anything. 2. Replacetime(0)
withrandom_device
, it works. Interesting.â Rick
41 mins ago
3
Using
time(0)
the seed values aren't that different, unlike usingstd::random_device
. If you still wanted to usetime(0)
you could calle.discard(n);
wheren
is the number of steps you want to advance the generator's state. If effect, throwing away the initialn
values.â Blastfurnace
38 mins ago
1
If
std::default_random_engine
is implemented asrand
, thentime(0)
is not going to produce vastly different seeds which will lead to a random sequence that is close the the last one. Stay away fromstd::default_random_engine
as you don't know what it is. Unless you really need performance I would default to using astd::mt19937
as your go to PRNGâ NathanOliver
35 mins ago
2
It really depends on the generator. It's conceivable that the most-significant bits of the seed have a greater effect on the generator's initial state. My advice, use a better seed like
std::random_device
and maybe usediscard()
to "warm up" the generator before use.â Blastfurnace
19 mins ago