Hello again! In this video, we are going to look at how to get a random number in older versions of
C++. C has a function called rand(), which C++ has inherited.
This is a pseudo-random number generator, so it will generate a sequence of numbers which look random.
And each time we call rand(), we get the next number from the sequence.
If we get to the end of the sequence, then it just goes back to the beginning, and starts again.
The return value is an integer between 0 and RAND_MAX. And RAND_MAX is chosen by the implementation,
so there is no standard.
So this is what we do,
to get a random number. And your random number today is 41. And that is OK if you want some random number between 0
and RAND_MAX, whatever that is. But you may want a different range of values.
And in that case, you have to do some arithmetic and rescale the result.
For example, if we want a floating point number between 0 and 1, we divide the return value
by RAND_MAX.
But we have to be careful, because these are both integers. So that will be an integer division. So we only
get 0 or 1.
So we need to make sure we do a floating-point division, and to do that, we convert the result of
rand() to a floating point number.
If we want the range to start from a different value, then we have to add that, after re-scaling.
So if we want integers between 1 and 100, we have to re-scale,
to get integers between 0 and 99, and then add 1.
So let's try that out.
So that is a floating-point number between 0 and 1.
And then we have 10 integers between 1 and 100.
And if I run it again, we get the same numbers.
If we want different random numbers, we have to "seed" the generator.
We do that by calling srand().
We pass the seed number as
the argument. And the traditional practice here is to pass the current time, because what could be more
random than the time?
There is no way of guessing that, is there (!?)
There is another C function, called time().
If we call this with argument 0, it will return the current time, as a very large integer.
The number of seconds since 1970. And then that can be used to seed the random number generator.
In addition to the obvious problem, the value returned by time only changes once per second.
So if you have another program which starts up at the same time, it will have the same seat and it is
going to generate the same sequence of random numbers.
And this does make it a bit easier for people who are trying to guess the sequence.
So here is the same code again, with our call to srand(), using the current time as the seed.
So if I wait one second, I should get different numbers.
I do not think I can type this fast enough to do it twice in one second!
Maybe you could do that from the command line, actually. Something for you to try out!
OK, so that all seems pretty straightforward, so why does it need to change? If you are just writing
"toy" programs, then this approach is good enough.
If you are serious about random numbers, then there are quite a few issues.
The first one is this the implementer can use any algorithm they want.
There is no standard. And apparently there have been some pretty shocking ones in the past.
The present implementations are probably okay, but not great.
There's a common tendency to generate numbers which are not particularly random. Especially if you look
at the last few digits. And quite a few people do that.
So she gets a random number between 0 and 1000, say, they just chop off everything except the last
few digits.
That is not really a very good approach.
If we do not want numbers between 0 and RAND_MAX, we have to re-scale the range, and most implementations
are not very robust to this.
If you re-scale the range, it introduces what is called "bias", and that means that some numbers are more
likely than others.
So the results are not quite as random as they should be.
And finally, the sequence is too short, and it repeats too often, and guessing the next number is not
as hard as it should be.
So this is really not good enough for cryptography.
Okay, so that is it for this video.
I will see you next time.
Until then, keep coding!