generating random numbers(without repetition)

Hai,

Can anyone tell me how to generate random numbers from a set of numbers(for example in between 1 to 100)

I want to select any 50 random numbers out of that 100

that to without REPETITION.

U can give a starting or first number everytime when ever generating random numbers.

Siva

[333 byte] By [kvsprasadrao] at [2007-9-26 5:35:28]
# 1
you generate a number and store it (in an array)then you generate a second number that's not in that arraythen you generate a third number that is not in the array etc etc etcjust do a loop
JavaDen at 2007-7-1 13:44:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Random R=new Random();

int[] allNumbers=new int[length];

int[] sorted=new int[length];

int randNumber;

for(int x=0;x<length;x++){

Arrays.sortInt(sorted);

while(true){

randNumber=R.nextInt(length);

if(Arrays.binarySearch(sorted,R><0){

allNumbers[x]=randNumber; sorted[x]=randNumber;

break;

}

}

}

lenght being the size of array you wanna make.

I suppose this code will work but i can see it becoming slow with larger arrays. If anyone got a better one ill be happy to take it :)

and yes, i know this thread is 4 years old but i have the same problem.

Uffeman at 2007-7-1 13:44:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
if(Arrays.binarySearch(sorted,R)<0){instead of if(Arrays.binarySearch(sorted,R><0){of course
Uffeman at 2007-7-1 13:44:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

What is slow about this algorithm is that you use Arrays.sort() to sort your "sorted" array every time through the loop.

Once you have a list sorted, it becomes relatively easy to maintain the sort.

the BinarySearch method returns an indication of where the searched for record would be found if it was present. You can use that to insert in place.

Alternatively, have a boolean array indicating whether or not you have used a number. Seeing as you have an array of that length anyway, why not.

Even better would be to use a Set (HashSet/TreeSet) to retain which numbers are selected. That way you also remove duplicates automatically.

// length sets the size of the set of numbers to draw from

// numRequired is how many unique random numbers to return

// assume numRequired <= length

// This algorithm works well when length is much greater than numRequired.

Set randomNumbers = new HashSet();

while(randomNumbers.size() < numRequired){

randomNumbers.add( new Integer(R.nextInt(length)));

}

An alternative algorithm is this.

- Take your array of numbers.

- scramble it - you can use Collections.shuffle() method.

- take first 'n' elements. They are random and unique ;-)

evnafets at 2007-7-1 13:44:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

thanks evna!

i got it after a while.

never used Collection thingies before (yes, im a noob), so i didnt know how to construct them using the classes implementing them =)

but i figure ill post the code so ppl can use it directly

java.util.List toScamble = Collections.synchronizedList( new LinkedList() );

//Collections.synchronizedList() probably overkill if you will only use it for a task this short

//java.util necessary if you are importing java.awt.* since it also has a List class

for(int x=(minimum value);x<=(maximum value);x++){

toScramble.add(new Integer(x));

}

Collections.shuffle(toScramble);

//then you can read them as objects with

toScramble.get(index);

//or as ints with

toScramble.get(index).intValue();

//or get all the values in an array with

toScramble.toArray(new Integer[]);

Uffeman at 2007-7-1 13:44:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Hai

Why can't we use vector to store the generated random numbers and use the

boolean contains()method to check whether the newly generated number is already in the vector.

I just make my suggestion.If this method has any flaw in it please make me aware of it.

Thank you

shan_pro at 2007-7-1 13:44:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Certainly you can use a Vector (or any other subclass of List) and use the contains() method.

However using a Set guarantees there will be no duplicates. So by using a Set you don't have to bother with the contains() method. If you try adding an item already in the list, the add method for Set just returns false.

So yes it works. I just think using a Set is a more elegant solution.

evnafets at 2007-7-1 13:44:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...