random() and randomize() functions are not working in any compiler except Turbo C++.
Here we'll tell you how to use random() function in visual studio, dev c++ and other compilers.
Here is code fragment :-
num = 1+ rand() % (n+1-1);
This code will generate a random number between 1 and n. You can use any value in place of n, provided that it is an integer.
Like for example,
num = 1+ rand() % (20+1-1);
This code will generate any number randomly between 1 to 20.
This will not generate the number 0.
Note :- This code will also run of turbo C++
Here is an program using this function,
#include
#include
#include
using namespace std;
int main()
{
int num;
for (int i = 0; i < 20; ++i)
{
num = 1 + rand() % (40 + 1 - 1);
cout << num<
}
_getch();
return 0;
}
Output of above code :-
You may get different output.
And there is no need to use randomize() function. Everytime it will generate different number.
Now problem regarding with random() function is solved.
No comments:
Post a Comment