How can you generate and then store a random number?

So I have to generate a random number between 1 and 6 and then store that same random number into an int variable. Eg, if the random number is 4, that must be stored into int i.

How on gods good earth can this be done?
Did you try the reference pages?

Example can be found here:
http://www.cplusplus.com/reference/cstdlib/rand/
Its actually very simple.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <time.h>

using namespace std;
int main()
{
     srand(time(0));
     int i = rand;
     cout<<i;
     return 0;
}
It's even better if you use C++11.

1
2
3
4
#include <chrono>
#include <iostream>
#include <limits>
#include <random> 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <typename T>
struct rand_int_in_t
{
  typedef T result_type;
  static constexpr result_type min() { return 0; }
  static constexpr result_type max() { return std::numeric_limits <T> ::max(); }

  std::mt19937 rng;

  rand_int_in_t(): rng( std::chrono::high_resolution_clock::now().time_since_epoch().count() )
  {
    rng.discard( 1000000 );  // MT needs some warmup
  }

  T operator () ( T a, T b ) { return std::uniform_int_distribution <T> ( a, b )( rng ); }
  T operator () () { return rng(); }
};
1
2
3
4
5
6
7
8
int main()
{
  rand_int_in_t <unsigned> rand_int_in;

  std::cout << "Ten random numbers in [1,20]:\n";
  for (int x = 0; x < 10; x++)
    std::cout << rand_int_in( 1, 20 ) << "\n";
}

Enjoy!
Ugh no. I've only started out using C++ so I don't know any of that stuff.
I've done input, output, ifs, and loops.

This program I have to do is rolling an imaginary dice and ouputting a random between 1 and 6 obviously. Then I have to store that number into an int. Then I have to roll it again and add whatever is outputted the second to the first value. Eg if the first roll is 5 and the second is 3, then that will add up to 8. I just need to know how to store the same random number into an int. I just need to know what to put in after this line of I code I have.

cout <<"You rolled " << 1+(rand()%6);

Do you use "cin >>" or is it something else?

Thanking you.
You can use this;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <time.h>

using namespace std;

int random(int low, int high);
int add(int x, int y);

int total_dice_value = 0;

int main()
{
	srand(static_cast<unsigned int>(time(0)));
	cout << "0. Exit" << endl;
	cout << "1. Roll" << endl;
	cout << "2. See end result" << endl;
	int choice;
	cin >> choice;
	if(choice != 0)
	{
		switch(choice)
		{
		case 1:
			{
				int temp_dice_value = random(1, 6);
				cout << "You rolled " << temp_dice_value << endl;
				cout << "Diced Value(" << temp_dice_value << ") + Total Diced Value(" << total_dice_value << ") is ";
				total_dice_value = add(total_dice_value, temp_dice_value);
				cout << total_dice_value << endl;
			}
			break;
		case 2:
			{
				cout << "Total dice value is " << total_dice_value << endl;
			}
			break;
		default:
			{
				cout << "Please enter a number shown on the list!" << endl;
			}
			break;
		}
		main();
	}

	return 0;
}

int random(int low, int high)
{
	return rand() % (high - low + 1) + low;
}

int add(int x, int y)
{
	return x + y;
}
Last edited on
Way to do someone's homework for him!
@MatiasMunk - Line 43: NEVER call main() recursively. It's not allowed.
Topic archived. No new replies allowed.