• 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

Question about Random Number Generation...

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I need to generate 10 random numbers without repeating the same number again. This is what I need to achieve: I have a set of 100 questions and each time the test taker wants to take a test, the program should generate 10 questions from the available 100 question set. So, far i am able to generate random numbers without zero in the following way. But, how to avoid repeating the same number again? Any ideas or code will be appreciated.

import java.util.Random;

public class RandomTest
{
public static void main(String args[])
{
Random generator = new Random();
int rnumber = generator.nextInt( 100 )+1;
// what is the code to eliminate the repetition...?
System.out.println("Random Number: "+ rnumber);

}

}
[ May 23, 2006: Message edited by: KiranKumar Gogineni ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about using some kind of Collection? You can check to see if a Collection already contains an element.
 
KiranKumar Gogineni
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a newbie to Collections. Could you please outline the psuedocode or approximate code so that I can explore the exact collection and try to implement?
Thanks for your idea.
Regards,
Kiran
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> I have a set of 100 questions

use Collections.shuffle(..) to mix 'em up,
then take the first 10 from your set
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alternatively, you can take advantage of the fact that collections that implement the Set interface don't allow duplicates:

[ May 23, 2006: Message edited by: Garrett Rowe ]
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if you use a TreeSet, they will be ordered.
 
KiranKumar Gogineni
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your ideas. I will try to implement it. In the meanwhile, if anybody happened work on this kind of logic, please send me the code snippet that uses collections.
Regards,
Kiran
 
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
here's a simple shuffle

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And here's an example using a Set
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kiran,
Try this. It will serve your purpose.

import java.util.*;
class ShuffleTest
{

public static void main(String[] args)
{
ArrayList<Integer> list = new ArrayList<Integer>();

for(int x = 1; x < 101; x++)
{
list.add(Integer.valueOf(x)); //Adds 100 numbers to the list
}
Collections.shuffle(list); //This shuffles the list

for(int x = 0, y = 11; x < y; x++)
{
System.out.println(list.get(x)); //This gets the first 10 from the list
}
}


}
 
KiranKumar Gogineni
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much guys for your quick help.
Thanks a lot.
Kiran
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic