Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (686)
Games in Android Showcase (198)
games submitted by our members
Games in WIP (758)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Generating unique random numbers.  (Read 2191 times)
0 Members and 1 Guest are viewing this topic.
Offline ShannonSmith
« Posted 2011-08-01 04:59:45 »

My current project involves a lot of procedural generation and I can't come up with a good way of generating a set of unique random numbers. Say I had to pick N random numbers between A and B with the guarantee of no duplicates how would you go about it? Keeping in mind N and B could be arbitrarily large discounting the inelegant obvious solution.
Offline Scarzzurs
« Reply #1 - Posted 2011-08-01 07:18:04 »

You could try generating a list with numbers A to B and drawing from it randomly (removing the element).

Or, you can pre-generate and shuffle the list in order to avoid the cost generating randomness later.
The shuffle method of the Collections class runs in linear time, so it should work even for large lists.

I suppose an ArrayList with predefined capacity would be a good choice for the storage in either case.

Hope these methods are adequate :-)

- Scarzzurs

My games and Projects:,
Old website
Offline philfrei
« Reply #2 - Posted 2011-08-01 07:58:22 »

Generate random number, put it into a HashSet (an unordered collection that rejects duplicates). If it is allowed into the HashSet, it is unique, and thus can be used.

If it DOESN'T go in, you could just generate another and try again, but there's no guarantee you will ever succeed in creating your set!

So, instead of that, try incrementing the number that collided and keep trying until you succeed. It seems to me a simple increment by 1 is OK, but it might be better to pick a large prime number that is not a factor of the size of the sample set, so that it jumps around more. The main thing is that whatever your increment, it should guarantee eventually touching each possible value. Then, as long as N is smaller than B-A, you are guaranteed of completing the task.

For example, if you sample set is 121, you wouldn't want to use an increment of 11, but 7 or 13 would be fine.

music and music apps:
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jono
« Reply #3 - Posted 2011-08-01 09:30:56 »

It might be easier to first generate an ordered list of N unique numbers that are between A and B, and then take randomly from that (or shuffle it after it is generated).

The ordered list can then be thought of as N+1 "gaps" between each number, where each gap is >= 1 and the sum of the gaps is B-A. If you can work out what the probability of each gap size is then you could create the list recursively:

List gaps(int n, int a, int b){
  if(n == 1)
      return {random(b-a)};
  int nextGap = randomGap(n,a,b);
  return {nextGap} + gaps(n-1,a+nextGap,b);

I don't know what the probability distribution of "randomGap" should be but I bet a statistician or wikipedia will.
Offline nsigma
« Reply #4 - Posted 2011-08-01 09:52:31 »

Keeping in mind N and B could be arbitrarily large discounting the inelegant obvious solution.

What inelegant obvious solution do you actually have in mind???  Just wondering if that was a HashSet from JDK because of object overhead, or the brute force approach, or something else?  If HashSet you could consider philfrei's post but use one of the various HashSet solutions that work directly with primitives.

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline ShannonSmith
« Reply #5 - Posted 2011-08-01 17:22:02 »

Inelegant would be anything where you can't predict how long the algorithm will run (such as re-picking on collisions). Generating the complete set from A to B, shuffling and selecting the first N would be fine for a small set but B can be arbitrarily large.
Offline Riven

« JGO Overlord »

Medals: 1238
Projects: 4
Exp: 16 years

Hand over your head.

« Reply #6 - Posted 2011-08-01 17:40:46 »

You can easily implement two strategies:

If there are few possible values, use a list and shuffle it, if it's big, use a set of values already returned.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline pjt33

« JGO Spiffy Duke »

Medals: 40
Projects: 4
Exp: 7 years

« Reply #7 - Posted 2011-08-01 22:38:41 »

On the assumption that B >> N:

Take a self-balancing tree which gives a count of the size of each subtree. Store the numbers already picked in the tree. To generate a new number:

int x = A + rnd.nextInt(B - A + 1 - tree.size());
int skipped = 0, skip = 0;
while ((skip = tree.countLE(x)) > skipped) {
    x += skip - skipped;
    skipped = skip;
return x;

If you're worried about long runs of taken numbers, you could complicate things even more...
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

roseslayer (498 views)
2016-08-06 11:43:29

roseslayer (456 views)
2016-08-06 09:43:11

xTheGamerCodes (529 views)
2016-08-04 15:40:59

xTheGamerCodes (524 views)
2016-08-04 15:40:24

orrenravid (870 views)
2016-07-16 03:57:23

theagentd (949 views)
2016-07-11 14:28:54

Hydroque (1045 views)
2016-07-06 05:56:57

Hydroque (1030 views)
2016-07-03 08:52:54

GrandCastle (846 views)
2016-07-01 09:13:47

GrandCastle (642 views)
2016-07-01 09:09:45
Rendering resources
by Roquen
2016-08-08 05:55:21

Rendering resources
by Roquen
2016-08-08 05:52:42

Rendering resources
by Roquen
2016-08-08 05:50:38

Rendering resources
by Roquen
2016-08-08 05:49:53

Rendering resources
by Roquen
2016-08-08 05:32:39

Making a Dynamic Plugin System
by Hydroque
2016-06-25 00:13:25

Java Data structures
by BinaryMonkL
2016-06-13 21:22:09

Java Data structures
by BinaryMonkL
2016-06-13 21:20:42 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!