Generating random numbers

• 09-01-2010, 10:52 AM
chinedum
Generating random numbers
Good day people,pls i need help developing a subroutine of function that generates 15 random integer numbers between 0 AND 75,but the caught is that once a number has been generated ,e.g 2 ,that number is never repeated or generated again,if it is the subroutine calls math.random until it generates a number that hasnt been generated before.How do i do this,do i create variables to hold each generated integer and then test that value against the new generated interger...pls i need help with the code
• 09-01-2010, 11:02 AM
XmisterIS
Off the top of my head, you could store the numbers in a vector and check the vector in a while loop each time you want to generate a new number (e.g. "while number exists in vector generate another"). You could start the while loop by initially assigning the first element of the vector to your random number, so you don't have two calls to math.random (messy, IMHO!)
• 09-01-2010, 11:39 AM
Tolls
You only have 75 numbers (76 if it's inclusive) so...

Build a single List containing all those numbers.

Then list.delete() one using a random number 0 to list length for the index. Store the result of the delete in your results List.
Do this 15 times.
• 09-01-2010, 11:46 AM
JosAH
Quote:

Originally Posted by Tolls
You only have 75 numbers (76 if it's inclusive) so...

Build a single List containing all those numbers.

Then list.delete() one using a random number 0 to list length for the index. Store the result of the delete in your results List.
Do this 15 times.

Don't forget the Collections.shuffle( ... ) method; as in:

Code:

```public List rand15() {   List<Integer> list= new ArrayList<Integer>();   for (int i= 0; i < 75; list.add(i++));   return Collections.shuffle(list).subList(0, 15); }```
kind regards,

Jos
• 09-01-2010, 11:47 AM
siva_vs_siva
import java.util.ArrayList;
import java.util.Random;
public class Generate {
public static void main(String args[])
{
int count=0,gen;
ArrayList array=new ArrayList();
Random random=new Random();

while(count!=15)
{
gen=random.nextInt(100);
if(gen>0&&gen<=75)
{
if(!array.contains(gen))
{
System.out.println(gen);
count++;
}
}
}
}

}
• 09-01-2010, 11:52 AM
siva_vs_siva
// May be this also good

import java.util.ArrayList;
import java.util.Random;
public class Generate {
public static void main(String args[])
{
int count=0,gen;
ArrayList array=new ArrayList();
Random random=new Random();

while(count!=15)
{
gen=random.nextInt(75); //up to 75
if(!array.contains(gen))
{
System.out.println(gen);
count++;
}

}
}

}
• 09-01-2010, 12:13 PM
Tolls
Quote:

Originally Posted by JosAH
Don't forget the Collections.shuffle( ... ) method; as in:

Code:

```public List rand15() {   List<Integer> list= new ArrayList<Integer>();   for (int i= 0; i < 75; list.add(i++));   return Collections.shuffle(list).subList(0, 15); }```
kind regards,

Jos

Curses!
I always forget shuffle()!

I have seen that so many times you'd think I'd remember it by now...:)
• 09-01-2010, 01:35 PM
Norm
• 09-01-2010, 02:01 PM
Tolls
Arrgh!
I'm stuck in an infinite loop!
• 09-21-2011, 05:03 AM
Fubarable
Re: Generating random numbers
Moderator action: spam post deleted.