Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Problem with random number in java

  Asked By: Kaua    Date: Feb 21    Category: Java    Views: 837
  

I want to make a random number between 1-100
But i want random only happen first time when my program run, when i
run again the number sort.

Ex : first time i run : 65, when i run again 66, when i run again 67

Share: 

 

7 Answers Found

 
Answer #1    Answered By: Lurline Fischer     Answered On: Feb 21

Try this code to check or test your random  number generator.


public static void main(String[] arg) {
Random rand = new Random();
long t = 0;
long total=0;
for (int k = 0; k < 10; k++) {
t=0;
for (int i = 0; i < 100; i++) {
i = rand.nextInt(100);
System.out.println(i);
t++;
}
total=total+t;
System.out.println("instances:" + t);
}
System.out.println("total instances:" + total);
}

 
Answer #2    Answered By: Alfonsine Miller     Answered On: Feb 21

You don't say which part of this is giving you trouble - i.e. is it the
generation of the random  number for the first run, or the storage of this random
number in a file for use by the next run, or working out whether it is the first
run of the program  (random number  wanted) or a subsequent run  (non-random number
wanted)? Please expand on where your problem  lies.

To hold the value between runs, you will need a file.

To work out if this is the first run of the program, probably the best way would
be to check whether the above file exists yet. If it doesn't, then it can be
assumed that this is the first run. If it does, simply read the number from the
last run, add 1 and write it back.

 
Answer #3    Answered By: Fedde Bakker     Answered On: Feb 21

Random rand = new Random();
i = rand.nextInt(101);
.........................
but also your numbers are random.

There are no difference between series

...65,67,68...
...67,78,12....

they are both random  by the theory

 
Answer #4    Answered By: Taylor White     Answered On: Feb 21

which random  methom do u use? util or math ?

 
Answer #5    Answered By: Cay Nguyen     Answered On: Feb 21

You need to initialize the random  Class with a seed, if you don't do
it, the seed is base in the time  and it is diferent each time.

use a fixed long for seed
Random random = new Random( 10 );
int number  = random.nextInt();

 
Answer #6    Answered By: Corbin Jones     Answered On: Feb 21

or Random rnd = new Random()
int number  = (rnd.nextInt()) % 10 );
then the number will be between 0-10;

 
Answer #7    Answered By: Taylor Evans     Answered On: Feb 21

Yes, or use the overloaded "nextInt" method

// number  betwwen 1 y 100 inclusive
Random random  = new Random( 10 );
int number = random.nextInt( 100 ) + 1;

From
java.sun.com/.../Random.html
int nextInt(int n)
Returns a pseudorandom, uniformly distributed int value
between 0 (inclusive) and the specified value (exclusive), drawn from
this random number generator's sequence.

 
Didn't find what you were looking for? Find more on Problem with random number in java Or get search suggestion and latest updates.




Tagged: