• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating random Unique values

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, there's always brute force, which for a ten-element array, is probably the best solution:

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, Jan's right. You didn't tell us it was 10 numbers out of a small range.
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so a collection is like a vector, list, or Set? Which should i use?
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic