• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random Numbers That Do Not Repeat  RSS feed

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We decided to write a Quiz Application:

We need a Random Generator that does not repeat numbers, so that questions are not asked again...
our first idea, was that we could throw used questions in an array and verify against that... it was not the best idea to use arrays.
our second idea, was that we could use ArrayList, this worked out pretty well, but we still cant figure out the logic...

I'll post whatever we have done till now, any help appreciated:


The problem, is with class "Gen", the thing is that the compiler tells me I have to return something or the other, but I cannot instruct the compiler that the return statement is in the else clause!


 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Call Collections.shuffle on your List of questions and then just loop through it.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Random numbers can repeat; that is part of the definition of randomness. What you want is called random selection from a declining population or something similar.
 
Sheriff
Posts: 22845
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want your original code to compile then replace line 11 by "return returnRandom(al);".

However that isn't the only error in that code. I suggest you follow Joanne's suggestion instead.
 
Lokesh Poovaragan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried what you have suggested, but the thing is, I did not get the hang of Collections.shuffle, anyway, I remember in python there was something called as sets,
apparently it is there even in Java, So i did it using that, but I have come across a different hurdle :


Once you run the program, you are supposed to give a command line argument (i give like 150).
after that the program prompts you to enter how many random numbers you want, if you enter a number below 20 it works, else if you enter 20 or more, nothing happens.
if i enter 10, the program works but with no randomness in it.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm surprised it works with any number above 10.
This line set x to an integer between 0 and 9. As a Set cannot contain duplicates that would mean you can never have more than 10 numbers in your set, which means that this line can never be false for any value of foo above 10.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Input 150, and that code is going to try and find 150 different random numbers between 0 and 9. That could take quite a long time!

I think you need to refine your requirements.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lokesh Poovaragan wrote:I did not get the hang of Collections.shuffle

It's pretty simple and avoids the need to generate all these random numbers. Why don't you show us what you tried and I'm sure someone will be able to help.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Input 150, and that code is going to try and find 150 different random numbers between 0 and 9.

That's not quite what he's doing. The 150 is used in the loop on line 12, but I can't see any purpose to that loop.
The number of random numbers required is read in on line 9.
But you're right that any number above 10 (not sure where he got 20 from) is going to cause an infinite loop.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Matthew Brown wrote:Input 150, and that code is going to try and find 150 different random numbers between 0 and 9.

That's not quite what he's doing. The 150 is used in the loop on line 12, but I can't see any purpose to that loop.
The number of random numbers required is read in on line 9.
But you're right that any number above 10 (not sure where he got 20 from) is going to cause an infinite loop.

It is, effectively, though. It will iterate until the size of the set has reached the value input - that's the loop on line 11. There are two different inputs - one as a command-line argument and one from System.in - which may be one of the causes of confusion.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In this way you will newer repeat questions if you have enough of them.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nazarii Bardiuk wrote:In this way you will newer repeat questions if you have enough of them.

I hate to say, but that is quite unnecessary. As Joanne has been trying to point out for quite a few posts now, Collections.shuffle() will do exactly what Lokesh wants; particularly if you already have an ArrayList of questionNumbers. The command is simply:
Collections.shuffle(questionNumbers);

Winston
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:particularly if you already have an ArrayList of questionNumbers.

Or even better, an ArrayList of questions.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Or even better, an ArrayList of questions.

Very true

Winston
 
Lokesh Poovaragan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never realized the simplicity of Collections.shuffle!
I thought that I was supposed to throw number's into a list, and use Collections.shuffle() to shuffle the numbers, then pick one out randomly.

That was untill I realized List's can contain both Number's as well as Text.

Now all I did was threw the question's into a List. and did Collections.shuffle() on them.
then I print them, every time I run the Program, the list keeps changing, exactly What I needed!

Thanks A Lot Everyone!

Here's the final Code :
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lokesh Poovaragan wrote:That was untill I realized List's can contain both Number's as well as Text...

Not for ought is she known as "St. Joanne of the shuffle".

But well done. Isn't it nicer when you "get it" for yourself?

Winston
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Not for ought is she known as "St. Joanne of the shuffle".

That's one of the more polite things I've been called.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!