### 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.
 ``1234567891011121314151617181920212223242526272829303132333435`` `````` #include #include #include #include #include using namespace std; int doublethenumber () { int theValue; cout <>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.
 ``12`` ``````cout <

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();``

 ``123456`` ``````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.

 ``123456`` ``````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.

 ``12`` ``````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.

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

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

 ``1234`` ``````int firstDigit = (random3 / 1) % 10; int secondDigit = (random3 / 10) % 10; int thirdDigit = (random3 / 100) % 10; ...``````
Last edited on
Topic archived. No new replies allowed.