1#include <iostream>
2#include <stdlib.h>
3#include <time.h>
4using namespace std;
5
6int main()
7{
8 int num;
9 srand(time(0));
10 num = rand() % 10 + 1;
11 cout << num << endl;
12}
13
1#include <cstdlib>
2#include <iostream>
3#include <ctime>
4
5int main()
6{
7 std::srand(std::time(nullptr)); // use current time as seed for random generator
8 int random_variable = std::rand();
9 std::cout << "Random value on [0 " << RAND_MAX << "]: "
10 << random_variable << '\n';
11}
1/* rand example: guess the number */
2#include <stdio.h> /* printf, scanf, puts, NULL */
3#include <stdlib.h> /* srand, rand */
4#include <time.h> /* time */
5
6int main ()
7{
8 int iSecret, iGuess;
9
10 /* initialize random seed: */
11 srand (time(NULL));
12
13 /* generate secret number between 1 and 10: */
14 iSecret = rand() % 10 + 1;
15
16 do {
17 printf ("Guess the number (1 to 10): ");
18 scanf ("%d",&iGuess);
19 if (iSecret<iGuess) puts ("The secret number is lower");
20 else if (iSecret>iGuess) puts ("The secret number is higher");
21 } while (iSecret!=iGuess);
22
23 puts ("Congratulations!");
24 return 0;
25}
1// Add thus to with the headers
2mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
3// Generate a function that will give values between l and r inclusive
4auto dist = uniform_int_distribution<int>(l, r);
5// get the random number using dist(rng);
1#include <iostream>
2#include <stdlib.h>
3#include <time.h>
4using namespace std;
5int main() {
6 srand(time(NULL) );
7 const char arrayNum[7] = {'0', '1', '2', '3', '4', '5', '6'};
8 int RandIndex = rand() % 7;
9 cout<<RandIndex<<endl;
10 return 0;
11}