Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Creating random Unique values

Sam Bluesman
Ranch Hand
Posts: 191
Hello.

Let say I need to create 10 random integers that are to be stored in an array, all of which being unique. e.g. int[] randomNumbers.

How can i generate a random number, and check that it is not already in the array of integers before putting it in the array, and asking the program to perform generating a random number again if the random numner is aready in the array?

I know hoe to generate the random numbers...its the checking i don't know how to do.

Thanks
[ February 16, 2006: Message edited by: Sam Bluesman ]

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24212
35
Well, there's always brute force, which for a ten-element array, is probably the best solution:

Sam Bluesman
Ranch Hand
Posts: 191
Thanks for that. I see what is going on in your algorithm. However, how do i use this to do what i want? i.e. how can i use this in loops to make it find another random number if it returns true?

I have never used return values like this before

Cheers
[ February 16, 2006: Message edited by: Sam Bluesman ]

Jan Groth
Ranch Hand
Posts: 456
if you look for a solution with o(n):

001yourfirstrandumnumber
002yoursecondrandomnumber

this way you are sure that no two numbers are identically.

hope it helps,
jan

Sam Bluesman
Ranch Hand
Posts: 191
Thanks for that, but I cant do that as I have to use only integers between 0 and 34 and they must be as such, as they reference something.

Sam Bluesman
Ranch Hand
Posts: 191
i suppose my question to Ernest was how do I take these true and false values and use them?

Jan Groth
Ranch Hand
Posts: 456
ok, cool.

in this case with such a few numbers i'd probably rather shuffle an array. why dont you put your 34 references in a appropriate data structure (probably a collection), shuffle it and return the first 10 elements?

you can use Collections.shuffe() for it...

many greetings,
jan

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24212
35
Yup, Jan's right. You didn't tell us it was 10 numbers out of a small range.

Sam Bluesman
Ranch Hand
Posts: 191
OK. I've been using arrays so far. How will trying to create a Collection data structure affect my program for this set of numbers?

Im confused. I have only ever used arrays before...

Thanks anyway guys.
[ February 16, 2006: Message edited by: Sam Bluesman ]

Sam Bluesman
Ranch Hand
Posts: 191
ok so a collection is like a vector, list, or Set? Which should i use?

Jan Groth
Ranch Hand
Posts: 456
hi sam,

okay, leaving all this "academic" o(n) stuff besides... :-)

what you should do is choose an appropriate collection type. i cannot give a full overview of java collections here, you should definetly google yourself. but roughly: a set cannot contain double elements, while a list can. do you have double references?

note that both of them are interfaces, so there are various implementations.

usually i use a ArrayList as a standard list, while a HashSet is my standard set. but again - take the time to study the api-description, its a big gain for your future...

create an ArrayList / Hashset, add your references, and call the static method Collection.shuffle(myListOrSet).