Seeding time and generating random numbers

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
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.
Cool, thanks.
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.
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.
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.
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).

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).
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.
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.
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!
@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().
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
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.