# Random rand() question

• 01-26-2009
Nathan the noob
Random rand() question
Code:

```srand(time(NULL));    if(input == "gameover")     {                           random =(rand() % (6 - 1 + 1)) + 1;                         if( random == 1)             {             cout <<"You Stumble along the dirt road and find a penny\n";             money = money + 1;               if(counter >= 1)               {               counter = counter - 1;               }             }             if( random == 2)             {             cout <<"You reach into yer moms pocket and pull out a penny\n";             money = money + 1;               if(counter >= 1)               {               counter = counter - 1;               }             }             if(random == 3)             {             cout <<"As you search your couch you find a penny\n";             money = money + 1;               if(counter >= 1)               {               counter = counter - 1;               }             }             if(random == 4 && counter == 0)             {             cout <<"As you wander helplessly down the street you get knocked in the head.\n"                   <<"You wake up later to find yourself in a alley and yer pockets empty\n";             money = 0;             counter = 5;             }             if(random == 5)             {             cout <<"You ask the mayor for bebe mooneys he gives u a penny\n";             money = money + 1;               if(counter >= 1)               {               counter = counter - 1;               }             }             if(random == 6)             {             cout <<"You Beg for mooneys and recieve a penny for a hard days work\n";             money = money + 1;               if(counter >= 1)               {               counter = counter - 1;               }             }```
For some reason that doesnt work for getting a number between 1 and 7 i dunt no how to get a number between that could someone point out how
Maby it does maby its something else but sometimes when it types it doesnt respond no output so im thinking thiers a hole somewere
• 01-26-2009
laserlight
It looks like it should work. You might want to post the smallest and simplest compilable program that demonstrates the problem.
• 01-26-2009
Nathan the noob
Code:

```#include<iostream> #include<string> #include<ctime> using namespace std; int main() {     int random;     int max;     int min;     while(random > 0)     {                 cout << "Enter a max: ";                 cin >> max;                 cout << "Enter a min: ";                 cin >> min;     srand(time(NULL));                random =(rand() % (max - min + 1)) + min;     cout <<"Hey the numbereno is " << random << "\n";     } }```
It works here to :P but doesnt seem to in the acual place
• 01-26-2009
laserlight
Quote:

Originally Posted by Nathan the noob
It works here to :P but doesnt seem to in the acual place

Ah, so you know that you have done something right. Compare the code here to the code in the "actual place". Check that the input values are as expected (e.g., is min < max?)

By the way, the pseudo-random number generator should be seeded once, not on each iteration.
• 01-26-2009
Nathan the noob
look at the top tell me if u see anything amiss
• 01-26-2009
laserlight
Quote:

Originally Posted by Nathan the noob
look at the top tell me if u see anything amiss

What happens if random == 4 and counter != 0?

Other than that it looks fine, though I would have used an if-else if-else chain or a switch instead. It is also not necessary to have the number generated be in the range 1 to 5 inclusive since it would work just as well being in the range 0 to 4 inclusive.

Oh, and remember to indent your code more consistently.
• 01-26-2009
Nathan the noob
Oh thats so thatevent doesnt happen to often and some times that pice of code when typed gives no responce and im not sure why
• 01-26-2009
laserlight
Quote:

Originally Posted by Nathan the noob
Oh thats so thatevent doesnt happen to often and some times that pice of code when typed gives no responce and im not sure why

What do you mean by "some times that pice of code when typed gives no responce"? As far as I can tell, assuming that input == "gameover", something will always be printed, except in the case where random == 4 and counter != 0, but according to you that would be expected.
• 01-26-2009
Nathan the noob
wait i get the problem now
its that when i t gets 4 and the counter is not 0 it doesnt respond well thanks any ways :P you were a big help
• 01-27-2009
cyberfish
and
Code:

`random =(rand() % (6 - 1 + 1)) + 1;`
is equivalent to
Code:

`random = rand() % 6 + 1;`
why -1 + 1?
• 01-27-2009
matsp
Code:

```            if( random == 1)             {                 ...             }             if( random == 2)             {                 ...             }             if(random == 3)             {                 ...             }             if(random == 4 && counter == 0)             {                 ...             }             if(random == 5)             {                 ...             if(random == 6)             {                 ...             }```
This could be written using a switch/case block instead:
Code:

```    switch(random)     {           case 1:             {                 ...             }             break;           case 2:             {                 ...             }             break;           case 3:             {                 ...             }             break;           case 4:             {                 if (counter == 0)                 ...             }             break; // etc.```
--
Mats
• 01-27-2009
Nathan the noob
Errrm thanks i am still looking for a way to use character or strings with switches would make stuff simpler
• 01-27-2009
matsp
Quote:

Originally Posted by Nathan the noob
Errrm thanks i am still looking for a way to use character or strings with switches would make stuff simpler

Well, you can't switch on a string (or character array) - for that, you probably want to build a table [such as vector, list or map] of a string and "something that tells you what to do" - the latter could be a functor object, a function pointer or simply a number that you then use a switch to find the "right action".

--
Mats
• 01-27-2009
dwks
Also, for what it's worth, it's best if you only call srand() once during the execution of a program.