1. ## Random Number Generator

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
Code:
```#include <iostream>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{
time_t now;
time(&now);
srand(now);
rand();
for (int i=0; i<1; i++) {
cout << rand() % 5 << endl;
}
system("PAUSE");
return 0;
}```

2. The easiest way to remember this is:
Code:
`rand()%(high - low + 1) + low`
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`

3. 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.

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

5. 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.

6. 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.

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

8. 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

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

afaik you need to force the program to give decimal points

10. One idea:
Code:
`cout << (int)((double) rand() * 4. / RAND_MAX + 1.) << endl;`

11. 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.

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:
Code:
```if (rand()%5 == 0)
{
//do stuff
}
else if (rand()%5 == 1)
{
//do stuff
}
else if...```
It won't be as accurate because you're actually making a new random number everytime in your if statement.

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

13. Thanks you guys for your suggestions. Hopefully i can use them to make a cool game somewhere down the line.
Once again thanks for the help.

14. This is good, but i was wondering how i could alter the code to get a random number from 1 to 4. Thats it
This solution should be sufficient for you (no duplicates).

Code:
```#include <iostream>
#include <ctime>

using namespace std;

int main()
{

time_t now;
time(&now);
srand(now);

/* Creates 'random' numbers between 1 and higestNumber*/
/* Note: Program crashes if howmanyNumbers > higestNumber */
int howmanyNumbers = 1;
int highestNumber = 2;
int i;

//Fill temp array with all possible numbers
int *temp = new int[highestNumber];
for (i = 0; i < highestNumber; ++i)
temp[i] = i+1;

//How many random numbers do you need?
int *randomNumbers = new int[howmanyNumbers];

for (i = 0; i < howmanyNumbers; ++i)
{
int random = rand() % highestNumber;

randomNumbers[i] = temp[random];

temp[random] = temp[highestNumber-1];
highestNumber--;
}

for (i = 0; i < howmanyNumbers; ++i)
cout << randomNumbers[i] << endl;

delete[] temp;
delete[] randomNumbers;

return 0;
}```
This produces quite random numbers.

15. 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.
Zach, Stoustrup says exactly the opposite in The C++ Programming Language, 22.7. He recommends another method:
int( (double(rand()) / RAND_MAX) * n)

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.