Seeding time and generating random numbers

Apr 9, 2015 at 10:53pm
If I seed a random number in main() with srand(time(NULL)) and generate random numbers in other functions with rand(), will the numbers I generate still be based off the seeded value, thus maintaining true randomness?
Last edited on Apr 9, 2015 at 10:53pm
Apr 9, 2015 at 11:08pm
true randomness?

This is not a thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void test()
{
	int x = rand() % 100 + 1;

	cout << x << endl;
}

int main()
{
	srand(time(NULL));

	test();
	system("pause");
}


But yeh, this will work and generate random numbers.
Apr 9, 2015 at 11:31pm
Cool, thanks.
Apr 9, 2015 at 11:35pm
closed account (2LzbRXSz)
There's a bit of a rumor going around that rand isn't exactly random (and by that I mean there seems to be a pattern to it), but those are just conspiracy theories fueled by odd compiler glitches.
Apr 9, 2015 at 11:41pm
There's a bit of a rumor going around that rand isn't exactly random (and by that I mean there seems to be a pattern to it), but those are just conspiracy theories fueled by odd compiler glitches.


Of course there's a pattern to it... it's a PRNG. The 'P' standing for "pseudo".

All PRNGs have a pattern.
Apr 9, 2015 at 11:41pm
There's a bit of a rumor going around that rand isn't exactly random (and by that I mean there seems to be a pattern to it), but those are just conspiracy theories fueled by odd compiler glitches.


Im actually having a hard time telling if theres some trolling in there :D Its not just a rumor, its a fact that it is not true randomness. It does have a pattern to it. for example, if you like open and closes the compiler all the time fast, it will increase by around 7 (depending on how fast you open it), I think its becuase its dictated by time obviously duh, becuase of the time(NULL) thing.
Apr 9, 2015 at 11:46pm
closed account (2LzbRXSz)
Well of course there's that, but I mean an obvious pattern. Some people report the first random generated number is ALWAYS divisible by 7 (which isn't true for me). Others say that if you give rand() a number that's divisible by 7 it will only ever give you 6 and 13 (which is an Xcode glitch but it hasn't happened to me in a very long time).

Apr 9, 2015 at 11:48pm
I don't see how anyone would figure out a pattern, considering the random value you get from rand() with srand() is based off an ever changing seed value(time itself).
Apr 10, 2015 at 6:48am
Bogeyman wrote:
I don't see how anyone would figure out a pattern, considering the random value you get from rand() with srand() is based off an ever changing seed value(time itself).

it will produce different patterns for different seeds.
but if you and i use the same seed, our sequence will be same! its not a characteristics of a "true random" number.
put it alternately: if i know your seed i can predict your whole sequence!
while, "true random" number sequences are impossible to predict/calculate.
Apr 10, 2015 at 7:27am
Is there really such a thing as "true randomness"? I think it's just what people say when they can't understand or predict the process going on.
Apr 10, 2015 at 8:42am
Is there really such a thing as "true randomness"? I think it's just what people say when they can't understand or predict the process going on.


i think there is no "true random" as nothing in the universe happens randomly.
is the result of coin flip(head/tail) random ? i think no!
Apr 10, 2015 at 8:47am
@Peter87
Do you mean in programming or life in general? If you mean in programming, well then of course nothing is truly random, as every process is based off a sequence of instructions. If you meant life in general, well then I think that's a philosophical question, whether rhetorical or not, that no-one can really answer.

Perhaps I should have worded my first post better. When I said 'true randomness', I was really just referring to the seeming randomness you get from seeding time with srand().
Apr 10, 2015 at 1:15pm
Bogeyman wrote:
I don't see how anyone would figure out a pattern, considering the random value you get from rand() with srand() is based off an ever changing seed value(time itself).


rand() produces a fixed, looping sequence of numbers.

The seed merely chooses a starting point in that sequence.

Once you see the entire sequence, rand() becomes 100% predictable.

This is true of all pRNGs.



Note that not all random number generators are pseudo random number generators. Some actually use external atmospheric noise to produce random numbers and therefore are considered "truly" random.

See: https://www.random.org/
Last edited on Apr 10, 2015 at 1:16pm
Apr 10, 2015 at 1:23pm
Thanks Disch, that looks quite interesting. Although I have no idea how you would incorporate atmospheric noise into a computer program.
Topic archived. No new replies allowed.