In many situations we all need to generate random numbers. Mainly in case of games we generate random numbers to provide random results to users depending on certain conditions.
cstdlib - C Standard General Utilities Library
stdlib – Standard General Utilities Library
This header provides several general purpose functions including random number generation, dynamic memory management, integer arithmetic, searching, sorting and converting.
Use rand() function in loops:
*/
Here we use the for loop to generate 8 different random numbers with the help of rand() function.
Generate Random numbers within a specific range:
Format:
First digit + (rand() % Last digit)
Still a problem?
Where we can seed a value between that parentheses and according to that it will generate random numbers as shown below:
*/
*/
Here we use the time(0) function, which will return the current second count, prompting the srand() function to set a different seek for the rand() function each and every time the program runs.
Thus we ultimately have the procedure in C++ to generate random numbers.
· Statistical modelling problems are also need random numbers to operate in a proper way.
· There is a false or pseudo random number generator in C++, stored within C++ Standard Libraries.
· To access that operator, we will use this rand function. We already lean about function in the preceding chapters.
· But to use the rand function, we need to include the <cstdlib> or you can also use <stdlib.h>.
cstdlib - C Standard General Utilities Library
stdlib – Standard General Utilities Library
This header provides several general purpose functions including random number generation, dynamic memory management, integer arithmetic, searching, sorting and converting.
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
cout<<rand(); //Output random number
}
· As this is already defined within the C++ standard library which is accessed with the help of <stdlib> header and thus there is no need to define the function separately here.
Use rand() function in loops:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
for(int a=1;a<8;a++){
cout<<rand()<<endl;
}
}
/*
Outputs
41
18467
6334
26500
19169
15724
11478
*/
Here we use the for loop to generate 8 different random numbers with the help of rand() function.
Generate Random numbers within a specific range:
· In case of situations where we need to generate random numbers within a specific range of numbers.
· Thus we use modulus (%) operator to generate random number within a specific range.
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
for(int a=1;a<8;a++){
cout<<1 + (rand()%20)<<endl;
}
}
/*
Outputs
2
8
15
1
10
5
19
*/
· First of all, we use for loop to generate multiple random numbers and then use a formula to generate random number within a specific range.
Format:
First digit + (rand() % Last digit)
· That is the number generated by rand() function will be divided first with the last digit of the range.
· And then the remainder will be added up with 1 to generate an output within that range.
Still a problem?
· Did you notice, that we are not generating true random numbers i.e. the numbers and sequence of the numbers are same for each respective argument.
· Thus providing the same output of random numbers after every compilation process for each argument.
· But for real case scenario we need true random numbers. We can manage that by using the srand() function.
Where we can seed a value between that parentheses and according to that it will generate random numbers as shown below:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
srand(56);
for(int a=1;a<8;a++){
cout<<1 + (rand()%20)<<endl;
}
}
/*
Outputs
2
7
4
15
16
19
3
*/
· But in this case also we need to put a different value every time between the parentheses of srand() function after each and every compilation process.
· So to solve this problem we use the time() function which utilise the number of seconds in our system’s time.
· As the number of seconds on our system time will definitely change after every second, thus we can use that to seed different number after every compilation process.
· To access that time() function, we need to include the <ctime> header from the standard library.
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
int main(){
srand(time(0));
for(int a=1;a<8;a++){
cout<<1 + (rand()%20)<<endl;
}
}
/*
Outputs
2
8
15
1
10
5
19
*/
Here we use the time(0) function, which will return the current second count, prompting the srand() function to set a different seek for the rand() function each and every time the program runs.
Thus we ultimately have the procedure in C++ to generate random numbers.