# I need non repeating random numbers

Page 1 of 1

## 6 Replies - 2924 Views - Last Post: 01 November 2011 - 07:45 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=252966&amp;s=e3f3a0ba92ccabcee8ade4d6a8ef2f71&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 smitha.r

Reputation: 0
• Posts: 9
• Joined: 22-September 11

# I need non repeating random numbers

Posted 26 October 2011 - 05:58 AM

I have got repeating random numbers.
```import java.lang .*;
import java.util.Random;
public class RandomNumberBounds {

private static void doRawRandomNumber() {

int rawRandomNumber;
int min = 32;
int max = 256;

for (int i = 0; i < 500 ; i++) {
rawRandomNumber = (int) (Math.random() * (max - min + 1) ) + min;
System.out.println("Random number : " + rawRandomNumber);
}

System.out.println("\n");
}

/**
* Sole entry point to the class and application.
* @param args Array of String arguments.
*/
public static void main(String[] args) {
doRawRandomNumber();
}

}

```

using gregorian calender is it possible to get non repeating random numbers.
using the current time and date in nano second.

Is This A Good Question/Topic? 0

## Replies To: I need non repeating random numbers

### #2 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: I need non repeating random numbers

Posted 26 October 2011 - 06:33 AM

Kind of defeats the purpose of randomness, doesn't it?

You may be able to program your own solution if you think of what you want as a group of randomly selected objects from a set of unique objects.

### #3 cfoley

• Cabbage

Reputation: 2180
• Posts: 4,571
• Joined: 11-December 07

## Re: I need non repeating random numbers

Posted 26 October 2011 - 06:33 AM

What does a gregorian calendar have to do with random numbers, unless you need random dates? Here are two really simple ways of generating non-repeating random numbers. They both use sets.

1. Start with an empty set. The set will eventually contain all the numbers you have already chosen. Choose numbers at random in the normal fashion. If the number is in the set, discard it and choose again. If it is not in the set it's one you haven't already chosen. Add it to the set and return it.

2. Start with your set full of all the possible numbers. This is great for ranges like 0-100 but terrible for large ranges. When you need to choose a number at random, remove a random one from the set and return that.

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: I need non repeating random numbers

Posted 26 October 2011 - 12:58 PM

Like for a Loto with non-repeating numbers ?

http://www.dreaminco...snippet2992.htm

### #5 baavgai

• Dreaming Coder

Reputation: 6204
• Posts: 13,316
• Joined: 16-October 07

## Re: I need non repeating random numbers

Posted 26 October 2011 - 01:02 PM

Your random number is between 32 and 256? Wont it have to repeat after 224 numbers?

If you have a finite set, then it's essentially shuffling cards. Make a list of all the numbers in the set and then shuffle it. Pick as many as you like off the top.

Alternately, make a list of the values you've already chosen and make sure the one you just chose isn't there. If it is, try again.

### #6 smitha.r

Reputation: 0
• Posts: 9
• Joined: 22-September 11

## Re: I need non repeating random numbers

Posted 01 November 2011 - 07:30 AM

``` import java.util.HashSet;
import java.util.Set;
import java.util.*;

public class NRand {
/**
* For testing
*/
public static void main(String[] args) {
// Return 6 unique numbers between 1 and 40
System.out.println(NRand.nUniqueRandom(100, 1,150));
}

/**
* Produce a Set of 'howMany' random integers in the range
* between 'lowerBound' and 'upperBound' inclusive
*
* @param howMany How many numbers do we want?
* @param lowerBound The lower bound of the range (inclusive)
* @param upperBound The upper bound of the range (inclusive)
*
* @return A Set containing integers
*/
public static Set<Integer> nUniqueRandom(int howMany, int lowerBound,
int upperBound) {
Set<Integer> numbers = new HashSet<Integer>(howMany);

while (numbers.size() < howMany) {
numbers.add((int) (Math.random() * (upperBound - lowerBound + 1)) +
lowerBound);
}

return numbers;
}
}

```

How to shuffle the random numbers in this code.

### #7 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11248
• Posts: 42,315
• Joined: 27-December 08

## Re: I need non repeating random numbers

Posted 01 November 2011 - 07:45 AM

You can't shuffle a Set. Plus you are already filling it with Random numbers. If you want to shuffle, you should use a List instead, and you can use the Collections.shuffle() method.