1#include <iostream>
2#include <cstdlib>
3#include <ctime>
4using namespace std;
5
6int main()
7{
8 srand(time(0));
9
10 for (int i = 0; i <= 10; i++)
11 {
12 cout << rand() % 10 << " ";
13 }
14}
15
1#include <iostream>
2#include <ctime>
3#include <cstdlib>
4using namespace std;
5int main()
6{
7 srand(time(0)); // Initialize random number generator.
8
9 cout<<"Random numbers generated between 1 and 10:"<<endl;
10 for(int i=0;i<10;i++)
11 cout << (rand() % 10) + 1<<" ";
12 return 0;
13}
14
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/* 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#include <iostream>
2#include <ctime>
3#include <cstdlib>
4
5using namespace std;
6
7int main()
8{
9 srand((unsigned)time(0));
10 int random_integer;
11 int lowest=1, highest=10;
12 int range=(highest-lowest)+1;
13 for(int index=0; index<20; index++){
14 random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
15 cout << random_integer << endl;
16 }
17}