Random numbers in C++: How to generate with rand(), srand() and time() functions

In this tutorial from our Progressive C++ course, we will learn how to generate numbers or any range of random, or random, numbers.

Random numbers with the function rand()

In many programs, it will be necessary to have random numbers.

For example, to draw a song in your player, to choose a random video from Youtube, to make a draw with Instagram followers, to choose a location on the game map that you created in C++, and etc. etc. etc.

The first way we have to do this is by using the rand() function, from the cstdlib library.
It will generate a number between 0 and the constant RAND_MAX.

First, let's look at the value of this RAND_MAX, which can vary from machine to machine:
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    cout<<"Interval: 0 - "<<RAND_MAX<<endl;

    return 0;
}
The result of the previous program on my machine was 2147483647.
And on your PC?

Now let's generate a random number:
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    cout<<rand()<<endl;

    return 0;
}
Now let's generate 6 random numbers:
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    int i;

    for(i=0 ; i<6 ; i++)
        cout<<rand()<<" ";

    return 0;
}
Did you notice anything?
Close your program. Open it again ... did you notice anything?

Yes, the random numbers are repeated! But, calm down, let's see how to solve this.

The function srand() and random seeds

In truth, there is no purely random number. There is a whole science, and studies and more studies and research on top of that.

Basically, some rule will have to be used to generate these numbers, which are actually pseudo-random. However, there is a way around this.

It is by providing numbers, ourselves, for C++ to take these values and generate different numbers from them. For this, we will use the srand() function, which accepts an integer as a parameter (an unsigned int, actually).

For example, do:
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    srand(2112);
    cout<<rand()<<endl;

    return 0;
}
See that now the value of the rand() has changed, it was no longer those numbers repeating.

Now it will generate other 'random' ones, as we supply 2112 as seed. But if you close and open the program, you will notice that the same 'random' values will appear.

Try this now:
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    int i;

    for(i=1 ; i<=10 ; i++){
        srand(i);
        cout<<rand()<<endl;
    }

    return 0;
}
Now everything was different, because at each iteration the seed that fed the srand() was different.
But even so, if you close and open it again, everything will be the same again, damn...

Function time() from library ctime

See how brilliant programmers are.

There is a function, from the ctime library, the time(), which when invoked returns the number of seconds since 00:00 on January 1, 1970. The trick is to use it as a seed generator, for srand().

Look:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    unsigned seed = time(0);

    srand(seed);
    cout<<rand()<<endl;

    return 0;
}
Open and close ... several times ... always different now, the number drawn!
After all, every second the team's return (0) is different, generating different seeds for srand(), which changes the rand (), which gives us different random numbers! PIMBA!

Choosing value ranges
1443561896
1944127442
107792574
1300800110
...
What kind of values are these? Very large ... we will rarely want to generate numbers that big. We want random ones that are 0 or 1 ... from 1 to 6 ... from 1 to 60 ... between 0 and 100 ... something like that, do you agree?

This is easily resolved with the rest of the division operator: %

For example, for:

Generate random ones that are 0 or 1
Just do: rand ()% 2
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    unsigned seed = time(0);

    srand(seed);
    cout<<rand()%2<<endl;

    return 0;
} 
  • Generate random from 1 to 10
Using rand ()%10: it will generate the numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9
So, just add 1, and we have numbers from 1 to 10.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    unsigned seed = time(0);

    srand(seed);
    cout<<1+rand()%10<<endl;

    return 0;
}


Nice, right?

No comments:

Post a Comment