Results 1 to 10 of 10
Thread: Generating random numbers
 09012010, 10:52 AM #1Member
 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
 09012010, 11:02 AM #2Member
 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!)
 09012010, 11:39 AM #3Moderator
 Join Date
 Apr 2009
 Posts
 12,520
 Rep Power
 21
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.
 09012010, 11:46 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,128
 Blog Entries
 7
 Rep Power
 24
 09012010, 11:47 AM #5Member
 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))
{
array.add(gen);
System.out.println(gen);
count++;
}
}
}
}
}
 09012010, 11:52 AM #6Member
 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))
{
array.add(gen);
System.out.println(gen);
count++;
}
}
}
}
 09012010, 12:13 PM #7Moderator
 Join Date
 Apr 2009
 Posts
 12,520
 Rep Power
 21
 09012010, 01:35 PM #8
See posts at Generating random numbers  Java Programming Forums
 09012010, 02:01 PM #9Moderator
 Join Date
 Apr 2009
 Posts
 12,520
 Rep Power
 21
Arrgh!
I'm stuck in an infinite loop!

Re: Generating random numbers
Moderator action: spam post deleted.
Similar Threads

Powerball Example (Generating Random Numbers)
By a7x75 in forum New To JavaReplies: 8Last Post: 02122010, 03:38 PM 
Need help generating random numbers atleast 2 characters apart
By ruby&oliver in forum New To JavaReplies: 12Last Post: 09232009, 09:14 AM 
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04192009, 05:50 PM 
Generating a random number
By oridov in forum New To JavaReplies: 2Last Post: 11292008, 06:12 PM 
generating random numbers in a 5x5 array.
By acidacid in forum New To JavaReplies: 3Last Post: 08142007, 03:44 AM
Bookmarks