PDA

View Full Version : Store random numbers



knowmysteeez
12-13-2007, 01:51 PM
In my programming class we were asked to write a program that writes 10 random numbers 100-10000. I got it to write the 10 numbers, but I cant seem to get it to go 100-10000! Could anyone point me in the right direction please?

yargo
03-15-2008, 10:30 PM
This is a good trick which is actually very useful.
If I were doing this this is how I would do this.

int x[] = new int[10];
Random rand = new Random();
for ( int i = 0; i<10; i++ ){
x[i] = rand.nextInt(99001)+100;
}
// This code will declare an integer array of size 10 and set the values of the integers to random numbers (0-99000 inclusive) + 100 so that the final set of randomly generated numbers is 100-100000 inclusive. Did I answer your question?

Yakman
03-16-2008, 11:15 AM
you might find it simpler to use Math.random(), which is random enough for most cases
the Random class is good if you want to specify a seed

omgnomorenames
03-25-2008, 08:35 PM
(int)(Math.random() * (9900) ) + 100;

I think thats what you want

In the program it would look like


for (int i = 0; i < 10; i++)
{
System.out.println ((int)(Math.random() * (9900) ) + 100);
}

pointer
09-20-2008, 08:55 AM
ty, helped with my fake deleting program lol