i want this code to do the action, which is generate a random number 1-6. i am totally lost here ive been looking at examples and this is what i have. i get the error function def is not allow here before "{" token.

void roll_hold( char remark)
{
if(remark=='r')
{
do
{
rand(0)% (7);
}while(remark== 'r');
}
if (remark=='h')
{
do
{
system("pause");
}while (remark=='h');
}
}
3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by Fbody

here is the code for generating random numbers

int which_Num;

unsigned seed = time(0);
srand(seed);

which_Num = rand() % 5;

The number 5 is the max number to be generated you can change it to whatever you need like 100 or 50 or in your case 6.

After that you just display which_Num and it will be a random number from 1 - 5 every time you run the program.

Edited by some: n/a

Votes + Comments
When posting code, make sure you know what you are posting and that it is correct.

y did u use
unsigned seed = time(0);
srand(seed);

here is the code for generating random numbers

int which_Num;

unsigned seed = time(0);
srand(seed);

which_Num = rand() % 5;

The number 5 is the max number to be generated you can change it to whatever you need like 100 or 50 or in your case 6.

After that you just display which_Num and it will be a random number from 1 - 5 every time you run the program.

Not correct. That code will generate 0-4 NOT 1-5.

@OP:
You need to "seed" the random number generator using srand() so that you do not get the same sequence of "random" numbers every time you call rand().

The most common method of seeding the generator is to call the time() function then give that return value to srand().

To generate a set of random numbers within a specific range, you must know your minimum value and the size of your range of values. You then take the modulus of the random number and the size of your range. After that, you add the minimum value to the result of the modulus operation. For example, to generate random numbers in the range 5-20:

#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
//declare local constants
const int MIN = 5;//the minimum allowable value
const int MAX = 20;//the maximum allowable value
const int RANGE_SIZE = (MAX+1) - MIN;

//declare a local variable
int someNum = 0;

//"seed" the random number generator w/ the value of time()
srand((unsigned)time(0));

//generate the random number
someNum = (rand() % RANGE_SIZE) + MIN;

return 0;
}

Note, you must add 1 to the maximum value of your range if you want the max value to be generated. Without adding 1, the algorithm will only produce MIN thru (MAX-1).

Edited by Fbody: n/a

Not correct. That code will generate 0-4 NOT 1-5.

@OP:
You need to "seed" the random number generator using srand() so that you do not get the same sequence of "random" numbers every time you call rand().

The most common method of seeding the generator is to call the time() function then give that return value to srand().

To generate a set of random numbers within a specific range, you must know your minimum value and the size of your range of values. You then take the modulus of the random number and the size of your range. After that, you add the minimum value to the result of the modulus operation. For example, to generate random numbers in the range 5-20:

#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
//declare local constants
const int MIN = 5;//the minimum allowable value
const int MAX = 20;//the maximum allowable value
const int RANGE_SIZE = (MAX+1) - MIN;

//declare a local variable
int someNum = 0;

//"seed" the random number generator w/ the value of time()
srand((unsigned)time(0));

//generate the random number
someNum = (rand() % RANGE_SIZE) + MIN;

return 0;
}

O i see how you are doing it. The way I did it was in the C++ book I had so naturally I thought that that was the only way of doing it.

Edited by some: n/a

In computing, rarely is there only 1 way to do things. The seeding I showed is essentially the same as yours. Your method just has an extra step that I left out.

I was actually commenting on your evaluation of the generated result(s), not your seeding. I suggest you do some research on the "modulus" operator.

Edited by Fbody: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.