stop repeat value from random

• 02-16-2013, 01:42 AM
nonybrighto
stop repeat value from random
please i want to make a program were i will generate random numbers. If a number is present, it should not be repeated...please help me.is it possible to do this?
• 02-16-2013, 01:44 AM
KevinWorkman
Re: stop repeat value from random
Sure, it's possible. Prepopulate a data structure with numbers, then shuffle it, and pull them out one by one. Or fill up a data structure as you generate random numbers, checking the newly generated numbers against what you've already seen.

What you do really depends on your context and the trade-offs you're willing to make. But chances are this is homework, so figuring out how to do it is the whole point of the assignment.
• 02-16-2013, 07:04 AM
nonybrighto
Re: stop repeat value from random
...i want to use arraylist to keep the datas and test if its already present.thanks for your help.i'll try it today and come back in case i have any problem. Please its not an assignment.i learn java on my own i want to make a game and i thought of sometin that needs that...:-)
• 02-16-2013, 07:22 AM
Fubarable
Re: stop repeat value from random
But it is homework regardless if it has been assigned by someone else or assigned by you to yourself. Half the fun is trying to figure it out, and we wouldn't want to ruin that. :)
• 02-16-2013, 09:08 AM
nonybrighto
Re: stop repeat value from random
Thanks bro i did it.i used arraylist.if you know any other shorter method u can drop it so i can have multiple knowledge about it....;-)

Code:

```import java.util.ArrayList; import java.util.Random; public class oncerandom {         /**         * @param args         */         public static void main(String[] args) {                                 ArrayList<Integer>  list =new ArrayList<Integer>();                                 Random rand= new Random();                                                 for (int i=0;i<=50000;i++)  //so that all numbers must be guessed...                 {                     int number=rand.nextInt(50);                                         if(!list.contains(number))                     {                             list.add(number);                     }                 }       for(int i=0;i<list.size();i++)       {               System.out.printf("%d ",list.get(i));       }         } }```
• 02-16-2013, 10:40 AM
JosAH
Re: stop repeat value from random
The nextInt(50) method call produces a number in the range 0 ... 49; it could produces the sequence 0, 0, 0, 0 ... (5000 times); it is not very likely, but it could happen en then you'll end up with a list with one single element (0); I don't think that is what you want. There is an easy way out though: have a look at the Collections.shuffle( ... ) method.

kind regards,

Jos
• 02-16-2013, 02:49 PM
nonybrighto
Re: stop repeat value from random
what will i shuffle?....the random or the arraylist?....i havn't used it before...
• 02-16-2013, 05:04 PM
KevinWorkman
Re: stop repeat value from random
Quote:

Originally Posted by nonybrighto
what will i shuffle?....the random or the arraylist?....i havn't used it before...

That's why you'd take a look at the API and tutorials to read about it before using it.

Once you learn what it does, take a step back and think about how it could be used to solve the problem.
• 02-16-2013, 08:26 PM
nonybrighto
Re: stop repeat value from random
thanks a lot to you all...i will do that now. God bless this site and its users