1. Member
Join Date
Sep 2010
Posts
1
Rep Power
0

## 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

2. Member
Join Date
Aug 2010
Posts
35
Rep Power
0
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!)

3. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18
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.

4. 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:

Java 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

5. Member
Join Date
Aug 2010
Posts
31
Rep Power
0
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++;
}
}
}
}

}

6. Member
Join Date
Aug 2010
Posts
31
Rep Power
0
// 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++;
}

}
}

}

7. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18
Originally Posted by JosAH
Don't forget the Collections.shuffle( ... ) method; as in:

Java 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...:)

8. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18
Arrgh!
I'm stuck in an infinite loop!