Forums Register Login

Creating random Unique values

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Well, there's always brute force, which for a ten-element array, is probably the best solution:

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
i suppose my question to Ernest was how do I take these true and false values and use them?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Yup, Jan's right. You didn't tell us it was 10 numbers out of a small range.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
ok so a collection is like a vector, list, or Set? Which should i use?
+Pie Number of slices to send: Send
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
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2228 times.
Similar Threads
Random number generation
Random Characters???
about random.next int();
storing a set of integers in an array
MySql Random Alphanumeric number
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:09:02.