wood burning stoves 2.0*
The moose likes Java in General and the fly likes Creating random Unique values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating random Unique values" Watch "Creating random Unique values" New topic
Author

Creating random Unique values

Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
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 ]

Moosey knows best
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Well, there's always brute force, which for a ten-element array, is probably the best solution:



[Jess in Action][AskingGoodQuestions]
Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
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

Joined: Feb 03, 2004
Posts: 456
if you look for a solution with o(n):

simply "add" a prefix / suffix to your number, like:

001yourfirstrandumnumber
002yoursecondrandomnumber

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

hope it helps,
jan
Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
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

Joined: Nov 21, 2004
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

Joined: Feb 03, 2004
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

Yup, Jan's right. You didn't tell us it was 10 numbers out of a small range.
Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
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

Joined: Nov 21, 2004
Posts: 191
ok so a collection is like a vector, list, or Set? Which should i use?
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
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).

if you have any problems, post your code snippet and we'll try to help you,


many greetings,
jan
 
Don't get me started about those stupid light bulbs.
 
subject: Creating random Unique values
 
Similar Threads
Random number generation
MySql Random Alphanumeric number
about random.next int();
Random Characters???
storing a set of integers in an array