# Thread: restrictions on randomly generated values?

1. Member
Join Date
Oct 2008
Posts
25
Rep Power
0

## restrictions on randomly generated values?

Is the a command in Java where you can disallow certain numbers from the range to be obtained with the Random class?

So if you want to generate 5 random integers between 1-50, but you do not want any of them to be 15 or 20, for example, is there a way to block them or do you have to make some sort of loop where you get a new number if one of the disallowed ones is obtained?

2. I think a simple loop would be the way to go here.

3. Senior Member
Join Date
Aug 2008
Posts
384
Rep Power
9
What Fubarable says, just use a while loop. (no for)

4. Member
Join Date
Oct 2008
Location
UK
Posts
65
Rep Power
0
Originally Posted by busdude
Is the a command in Java where you can disallow certain numbers from the range to be obtained with the Random class?

So if you want to generate 5 random integers between 1-50, but you do not want any of them to be 15 or 20, for example, is there a way to block them or do you have to make some sort of loop where you get a new number if one of the disallowed ones is obtained?
For your example, I would do as the others have suggested, and just have a loop which generates the number inside it and repeats until that number is neither 15 nor 20.

However, to take the general case, for me it would really depend on the circumstances; i.e. whether the code is part of an algorithm that has to be very fast, and what proportion of the range was excluded.

For example, if your range was 1-50, inclusive, but you wanted to exclude numbers in the range 5-44, then I would probably take a different approach. in this case, since there are only 10 permitted values then I would generate a random number between 1-10 and use a calculation to map this number onto the allowed values.

To take another example, suppose you wanted to exclude odd numbers, then I would simply generate a number between 1-25 and double it.

5. For the first suggestion; my question is how to map numbers into the allowed values. I'm not clear that how you going to do it.

For the second; I don't think it's a good choice actually. If you double the value, it's not a random value in sense, is it? Because you generating the second number, but effect with the initial value.

6. Member
Join Date
Oct 2008
Location
UK
Posts
65
Rep Power
0
Originally Posted by Eranga
For the first suggestion; my question is how to map numbers into the allowed values. I'm not clear that how you going to do it.

For the second; I don't think it's a good choice actually. If you double the value, it's not a random value in sense, is it? Because you generating the second number, but effect with the initial value.
busdude was asking a general question, but using a specific example, as I said in this example a loop would be the best way, but I give two other examples where I think it would not.

In my first example, let me make it clear how you would do the mapping:

Java Code:
```Random r = new Random();
int random = r.nextInt(10);
int mapped = (random<=3) ? random+1 : random + 41;
System.out.println(mapped);```
And what it does is this:

random-->mapped
0-->1
1-->2
2-->3
3-->4
4-->45
5-->46
6-->47
7-->48
8-->49
9-->50

In my second example, I said that odd numbers have to be excluded.

Java Code:
```Random r = new Random();
int random = r.nextInt(25);
int mapped = (random+1)*2```
And it maps like this:

random-->mapped
0-->2
1-->4
......
23-->48
24-->50

By definition, if any numbers are excluded then the distribution is not uniform.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•