• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

random number generating

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there can anyone help? I am attempting to generate 5 random numbers between 1 and 20 without duplicates.

So far I have the code
ARRAY_SIZE = 5;
int[]random = new int [ARRAY_SIZE];
for (int x = 0; x < random.length; x++)
random[x]=Math.abs(numGenerator.nextInt(20)) + 1;

therfore, i can generate the numbers but do not know how to prevent duplicates.

Would appreciate any help, thanks.Mark
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a simple way is to put the range of numbers into an arrayList (as Integers, not int's)
use Collections.shuffle() on the list
take the first 5 numbers form the list (elements 0 to 4)
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael,

Sorry for being clueless, but what do you mean when you say use "Integers, not ints"... int's are 32-bit numerical data-types... I though Integer in java-speak was a part of the API....
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayLists store objects
an 'int' is a primitive

so, instead of
int x = 10;
arrayList.add(x);
you add it like this
arrayList.add(new Integer(x));
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way i did it wasnt the best way, but it worked.

 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is pretty straightforward with java.util.Random class.


 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ummm... what happened to the "without duplicates" that the original poster specified? That's the problem Michael Dunn was solving.

Mark, note that if you are using JDK 5, the difference between an int and an Integer may be less obvious. You will want to learn more about autoboxing to make sense of this. If you're not using JDK 5, then never mind. Either way, if you're new to collections, you should probably read up on them.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Dunn:
ArrayLists store objects
an 'int' is a primitive

so, instead of
int x = 10;
arrayList.add(x);
you add it like this
arrayList.add(new Integer(x));



As of Java 5, "arrayList.add(x);" will compile just fine. This uses the new autoboxing feature. If you are interested, you should read up on it in the New Features and Enhancements section of the JDK docs.

Layne
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the range of numbers that you're making a random selection from is fairly small (like 1-6 or 1-100) then the shuffle() idea works pretty well. If the range is large (like any integer) then that technique is pretty wasteful. Another idea would be to store each randomly selected number in a Set until the Set contained the correct number of elements. Adding elements to a set makes sure that duplicates are ignored.

_M_
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since this is in Java in General (Beginner), I would note that beginners may well want to skip this post entirely. Go ahead, you're not missing anything overly important. Probably.

Mike: I think we'd want to look not just at the size of the range of numbers, but also at how many random numbers we're to generate. Let's say the range is 1-1000000. If we just need to generate 10 random numbers with no duplicates, then using a HashSet is certainly more efficient. But if we need to generate 1000000, the shuffle method is better (and uses a comparable amount of memory).

Of course the numbers get a lot worse for bigger ranges, and using a HashSet eventually becomes the only reasonable choice.

Also worth noting: on the crappy old 500 MHz machine I'm using at the moment, it takes 2-3 seconds to create an array of 1000000 Integers and shuffle them. For some applications that might be an unacceptable delay, while for others it's not worth worrying about. It's also possible to more than double the speed (and halve the memory consumption) by stealing the shuffle() algorithm from Collections (it's simple), and adapting it to operate on an int[] rather than a List.
[ January 18, 2006: Message edited by: Jim Yingst ]
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai
try my logic(it may bad)

java.util.Random data = new java.util.Random();
HashSet hs=new HashSet();
for(int i=0; ; i++)
{
int n = data.nextInt(20);
Integer nn=new Integer(n);
hs.add(nn);
if(hs.size()>=5)
break;
}
Iterator it=hs.iterator();
while(it.hasNext())
System.out.println((Integer)it.next());

}
 
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic