Random Number/ variables

I dont have any code yet, just a general question. I'm working on an assignment that will generate random numbers, then do things like doubling that number, reversing order, etc. and I cant find an answer for my question anywhere on the net (or my school book). Is it possible to generate a random number, and then assign that value to a variable? I tried doing this, didn't work so well.
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
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <random>
#include <string>

using namespace std;

int doublethenumber () {
    int theValue;
cout <<rand<<endl;
rand = theValue;
theValue = theValue * 2;
cout <<" That number doubled is "<<theValue<<endl;


}

int main()
{
    char again ='Y'; // I did not know how to loop the program so that the user could
    //make multiple tries without this variable. Program would only run once

    while (again == 'y' || again == 'Y')
    {
     doublethenumber();


      cout<<"Do you want to do another?"<<endl;
      cin>>again;
    }
    cout <<" Good bye"<< endl;
    return 0;
}
The comment at the char again line is for my teacher, he said to not use global variables, but I'm hoping since this is for functionality he will let it slide.
1
2
cout <<rand<<endl;
rand = theValue;

rand is a function that generates a number between 0 and RAND_MAX. Functions are called with parentheses.

cout << rand() << endl;

Assign rand to a variable:
 
int r = rand();


1
2
3
4
5
6
void doublethenumber() {
    int theValue = rand();
    cout << theValue << endl;
    theValue = theValue * 2;
    cout << "That number doubled is " << theValue << endl;
}

Note that I changed your return type from int to void because you aren't returning any value.

Also, when working with rand, you must seed it if you want different random values each program run. The usual way of doing this is by calling srand(time(NULL)); as the first argument in main.

1
2
3
4
5
6
int main()
{
    srand(time(NULL));
    
    // the rest of main goes here, no need to call srand again.
}
Last edited on
Ahh, thank you, Ganado. That pretty much helps with the entire assignment, since I have to create a lot of different functions, then do different things with random numbers generated. Appreciate it.
Now I am trying to add the first two numbers of a random number generated, I am guessing it is necessary to convert that number to a string first, so that I can separate the first and second with with substr, but my compiler wont let me do that. Its getting hung up on the line "firstdigit = str.substr(0);".

int random3 = ((1 + (rand() % 10)) + (1 + (rand() % 10)));// should give double digits
char firstdigit;
char seconddigit;
stringstream ss;
ss<<random3;
string str = ss.str();
int sum;

firstdigit = str.substr(0);
seconddigit = str.substr(1);
cout << "The random number is "<<random3<<endl;

cout <<"The first digit is "<<firstdigit<< "and the second digit is "<<seconddigit<<endl;
sum = firstdigit + seconddigit;
cout << "The sum of those numbers is "<< sum<<endl;

}
Last edited on
substr gives you a string. If you want a char you can use the subscript operator the same way you access elements in an array.

1
2
char firstDigit  = str[0];
char secondDigit = str[1];

Note that this gives you the character code for the digit. If you want the numerical value of the digit you need to subtract '0' from it.

1
2
int firstDigit  = str[0] - '0';
int secondDigit = str[1] - '0';

It is however possible to calculate it using only arithmetic operators.

1
2
3
4
int firstDigit  = (random3 / 1) % 10;
int secondDigit = (random3 / 10) % 10;
int thirdDigit  = (random3 / 100) % 10;
...
Last edited on
Registered users can post here. Sign in or register to post.