SRL Archive - Java Help and Tutorials

Store random numbers


knowmysteeez

2007-12-13 08:51:34

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

2008-03-15 18:30:20

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

2008-03-16 07:15:56

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

2008-03-25 16:35:34

(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

2008-09-20 04:55:01

ty, helped with my fake deleting program lol