programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Generate random numbers without certain numbers in the range

Joseph Alrawi
Greenhorn
Posts: 23
Hey!

I tried out doing number (generated randomly) != (another number) but that does not work. If I for example want a number between 1 and 10, but I do not want the number 5, what can I do in order to make this happen?

Thanks!

Pablo Jose Alvarez
Greenhorn
Posts: 17
1
How about a number between 1 and 4 OR a number between 6 and 10?

Joseph Alrawi
Greenhorn
Posts: 23
Pablo Jose Alvarez wrote:How about a number between 1 and 4 OR a number between 6 and 10?

Hey.

The problem is, if I'm working with many numbers I do not want, it will take lots of time coding that.

Pablo Jose Alvarez
Greenhorn
Posts: 17
1
• 1
How about creating an array with the desired numbers (excluding the ones you don't one) and then generate a random number, to pick a random index from the array?

Piet Souris
Master Rancher
Posts: 2044
75
That is a fine advice, straightforward to implement.

Another way would be to generate two Lists, ArrayLists for example.
Then, in list1 you put all possible numbers, for instance the numbers
1-100.
In your second list, you put all the numbers that you don't want.

Then you could issue 'list1.removeAll(list2)'.

Greetz,
Piet

Campbell Ritchie
Marshal
Posts: 56536
172
Another way to implement random choice from
between 1 and 4 OR a number between 6 and 10
That is nine numbers missing out 5.
Suggest: randomly‑chosen number between 1 and 9. If number greater than 4 add 1 to it.

For anything more complicated, I shall refine PS's suggestion:
• 1: Create a Set containing the larger range of numbers.
• 2: Create a Set to which you add all the numbers you wish to avoid.
• 3: Use the removeAll method as suggested (set difference) to create a Set with only the numbers you want.
• 4: Convert that Set to a List with something like this constructor.
• 5: Use a randomly‑chosen number as an argument to the get() method of the List.
• you should not go on about lots of code. If you have something complicated, that is a simple way to achieve it.

There is probably a way to create a Stream in Java8 to populate the set or list but I don't know how to.

fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
I boils down to the same basic problem...you have a bunch of "things", and you want to pick one randomly. The fact that they are non-consecutive integers is irrelevant. In every case:

• You need to be able to create a collection of all the things you care about.
• You need to know how many there are.
• You need to pick a random number between 1 and that size (or between 0 and less than that size)
• You need some way to map between the random numbers and the collections.

• Sometimes, step 4 is trivial - i.e. a random number between 1 and 10 maps quite easily to the numbers 1 - 10. even a random number from 26-35 can be mapped quite easily.

But what would you do if you had to select one out of a bunch of dogs? or Bank accounts? or ftp servers? The thought process should be the same, regardless of what the 'thing' you are choosing is.

Campbell Ritchie
Marshal
Posts: 56536
172
There is some discussion about how to generate random numbers here.

 It is sorta covered in the JavaRanch Style Guide.