Hey i have started this random number generator and i am having a problem. The code produces a random integer from 0 to 4. This is good, but i was wondering how i could alter the code to get a random number from 1 to 4. Thats it

Where high is the highest random number, and low is the lowest random number. So to get a range from 1 - 4:

Code:

rand()%(4 - 1 + 1) + 1

Or to shorten it:

Code:

rand()%4 + 1

08-16-2003

Ikurik

I tried your suggestion, and while it does accomplish the goal of excluding 0 it does something else also. I ran the program multiple times with your suggestion and the re sults i got were something like this:
1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4,1, 1,1, 2, 2, ect.
Basically what i am trying to say is that it doesnt look to random to me. It looks like it is just making some kind of sequence towards 4 and then starting over again at 1.

08-16-2003

Dark Nemesis

try giving it a larger amount of space like:
rand()%20+1;

08-16-2003

joshdick

Remember, your computer's random number generator isn't perfect. That's why it's called a pseudo-random number generator. What you can do is generate a random number between zero and rand_max. Then, if the number is in the first fourth of that range, return 1, and so on. That might yield seemingly more random results.

08-16-2003

ygfperson

Quote:

Originally posted by Ikurik I tried your suggestion, and while it does accomplish the goal of excluding 0 it does something else also. I ran the program multiple times with your suggestion and the re sults i got were something like this:
1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4,1, 1,1, 2, 2, ect.
Basically what i am trying to say is that it doesnt look to random to me. It looks like it is just making some kind of sequence towards 4 and then starting over again at 1.

Bad luck, I guess.
Try making this

Code:

for (int i=0; i<1; i++) {
cout << rand() % 5 << endl;
}

into this:

Code:

for (int i=0;i < 10; ++i) {
cout << rand()%4 + 1 << endl;
}

So it prints out more often per program execution.

08-16-2003

Ikurik

Ok Ok so if i give it a larger range to work with, i get what appear to be more randomized numbers. But i guess what i should say is that my goal is eventually to make a dnd style text adventure. And i need some kind of random number generator to act as a 4 sided die. Apparently the suggestions made earlier in this thread work well for numbers greater than 4 but for a 4 sided die are there any suggestions????

Am i being to picky? I mean will having the number almost sequenced like they are with the 1-4 generator really affect the gameplay? The last thing i want is you damage roll to increase untill it gets to 4 and then drop back down to one.

08-16-2003

Ikurik

I just had an idea but im not sure how to implement it. Since the numbers seem to be more random as the range increases. Could i possibly make a random number from 1 to 8, then divide that number in half and then round that number to the nearest whole? And if that is possible i really have no clue about how to go about it.

thanks

08-16-2003

Dark Nemesis

i = rand()%7;
i++;
i = i / 2;

afaik you need to force the program to give decimal points

08-16-2003

swoopy

One idea:

Code:

cout << (int)((double) rand() * 4. / RAND_MAX + 1.) << endl;

08-16-2003

funkydude9

Try this:

Code:

#include <iostream.h>
#include <stdlib.h>
#include <time.h>

int main()
{
srand(time(NULL));

int randnum;

for (int i = 0; i < 50; i++)
{

randnum = rand()%(100);

if (randnum <= 25)
randnum = 1;

else if (randnum <= 50)
randnum = 2;

else if (randnum <= 75)
randnum = 3;

else
randnum = 4;

cout<<randnum<<endl;
}

return 0;
}

Actaually that might make it worse...lol. :rolleyes:

Put this line of code in the beginning of main(), as it seeds the random number generator:

Code:

srand(time(NULL));

Also, make sure to store your random number in a variable. ie don't do this:

Originally posted by Zach L. Most of those solutions just try to cover up the problem without actually dealing with it. In fact, typically the low-order bits are the most "random". Swoopy's solution is probably the best, however.

for numbers from 0 to n-1. Note this does have a miniscule but nonzero chance (0.00000005% for 31 bits of randomness) of returning n itself, so a serious application would have to take this into account.