Search: | |||||

| ||||

Home » Forum » Java | RSS Feeds |

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

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);

}

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.

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

which random methom do u use? util or math ?

or Random rnd = new Random()

int number = (rnd.nextInt()) % 10 );

then the number will be between 0-10;

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.

Related Topics:

- Help in Random Numbers
- generating random numbers
- HW HELP! loop, histogram, random number
- Java Development Kit 1.2 and Java 3D 1.3.1 -Problems
- How do I validate Phone numbers in java/ email addresses
- large numbers in java
- Converting a "Stringed" Number to a Number
- Converting a "Stringed" Number to a Number
- Select random cell in list and highlight contents
- How to create random password for first time user in the web site
- Need codes for generating random equations
- random images
- problem of converting a number into words using double
- Write a script to generate a random number between 1 and 1000.
- Please help quick. Due tomorrow. random number array histogram!!
- prime number problem
- Add-Ins loaded into Excel
- J48 classifier with Java's WEKA
- Difference b/w java beans & enterprise java beans
- Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Contain
- Java: Convert Class -file to Java -file?
- java.sql.SQLException: java.lang.ArrayIndexOutOfBoundsException
- Reg, Executing a java code within java program
- Problems with java forums login
- Some problem in Java Applet