Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about Random Number Generation...  RSS feed

 
KiranKumar Gogineni
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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 ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • 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
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • 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: 2409
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
here's a simple shuffle

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And here's an example using a Set
 
Mallesham Karnati
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you very much guys for your quick help.
Thanks a lot.
Kiran
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!