470,810 Members | 1,444 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

Random Numbers

I know that you can scale random numbers down to a range that you want. For instance I know that I can generate a random number between [m,n] inclusive.
What I want to do is generate a random number between [m,n], BUT I want to exclude certain numbers in this range. For example let's say that I wanted to generate a random number between 4 and 10,but I want to exclude 6 and 8. How could I do that? Is it just as simple as something like this:

rand() !=6 && !=8;

I know that the above code is not correct,but am I on the right track?
Oct 24 '08 #1
2 1847
JosAH
11,448 Expert 8TB
Yep, you're on the right track. Simply generate another pseudo random number
if it hit one of the 'forbidden' numbers.

kind regards,

Jos
Oct 24 '08 #2
I know that you can scale random numbers down to a range that you want. For instance I know that I can generate a random number between [m,n] inclusive.
What I want to do is generate a random number between [m,n], BUT I want to exclude certain numbers in this range. For example let's say that I wanted to generate a random number between 4 and 10,but I want to exclude 6 and 8. How could I do that? Is it just as simple as something like this:

rand() !=6 && !=8;

I know that the above code is not correct,but am I on the right track?
Yes you are. You can just exclude 6 and 8 with a do while loop.
Oct 24 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by Nicholas Geraldi | last post: by
3 posts views Thread by Joe | last post: by
21 posts views Thread by Marc Dansereau | last post: by
5 posts views Thread by cvnweb | last post: by
104 posts views Thread by fieldfallow | last post: by
12 posts views Thread by Jim Michaels | last post: by
21 posts views Thread by chico_yallin | last post: by
13 posts views Thread by Peter Oliphant | last post: by
24 posts views Thread by pereges | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.