Help with random numbers.

So I am currently taking a computer science 1 course and I am having some problems with an assignment.

My problem is basically this. I need to generate a random number from 1-4. I am using the command rand()%5; to do this.
My problem is that this method includes the number 0 and I am not allowed to use 0.
I figured I would use a loop to eliminate 0, however, I am not sure how after recognizing the condition of ( Number < 1 ) to make the program recalculate the "Number" again without any input from the user.

I want the program to always come up with a random number from 1 to 4 even if 0 is the first number selected randomly.
In short, if "Number" is 0 I need to recalculate until "Number" is > 0

Any help would be appreciated. It seems so simple but I just can't figure it out and my project is due tomorrow. Thanks in advance.

Evan

You are right, it is fairly simple.
This rand()%5 gives five possible results in the range 0 to 4.
What you need is rand()%4; - this gives four possible results, 0, 1, 2 and 3. Almost there. Now just add 1, giving possible results 1, 2, 3 and 4.
Here: rand()%4 + 1;
Wow, thanks. So easy...
My Professor would have had us do some time of loop to eliminate 0 I just know it. He will probably be ticked when I use this method.

Thanks so much.
Topic archived. No new replies allowed.