# Random number

King-Chieh Wang

Greenhorn

Posts: 24

Sanyev Babu

Ranch Hand

Posts: 132

posted 12 years ago

The code above uses the random methord in the java.lang.Math class it generates a double random number between 0 and 1 including 0 and excluding 1. You can multiply the number with 5 and then add 5 to it to obtain a randum number between 5 and 10. Note that the number generated will be a double number if you want it to be int you must down cast it.

The code above uses the random methord in the java.lang.Math class it generates a double random number between 0 and 1 including 0 and excluding 1. You can multiply the number with 5 and then add 5 to it to obtain a randum number between 5 and 10. Note that the number generated will be a double number if you want it to be int you must down cast it.

Sanyev Babu

Ranch Hand

Posts: 132

Vicken Karaoghlanian

Ranch Hand

Posts: 522

posted 12 years ago

Sanyev-

In regards to your second post, you can always edit your first post instead of creating a new one.

King-Chiehm-

Although the solutions posted by Sanyev is correct however it is not accurate because the number 10 will never be reached, the closest number you'll get is 9.9999999.

So technically speaking, [5,10) will describe the answer more accurately.

[ March 09, 2004: Message edited by: Vicken Karaoghlanian ]

In regards to your second post, you can always edit your first post instead of creating a new one.

King-Chiehm-

Although the solutions posted by Sanyev is correct however it is not accurate because the number 10 will never be reached, the closest number you'll get is 9.9999999.

So technically speaking, [5,10) will describe the answer more accurately.

[ March 09, 2004: Message edited by: Vicken Karaoghlanian ]

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!

Carlos Failde

Ranch Hand

Posts: 84

posted 12 years ago

java.util.Random random = new java.util.Random();

int n = 5 + random.nextInt(6);

nextInt(6) returns numbers between 0 inclusive and 6 exclusive

int n = 5 + random.nextInt(6);

nextInt(6) returns numbers between 0 inclusive and 6 exclusive

Sanyev Babu

Ranch Hand

Posts: 132

posted 12 years ago

The above code will generate integer random numbers in the interval [5,10] both inclusive. Note that the random number in the interval [0,1) is multipled with 6 so we will get numbers in the range [0,6). Adding five to the value will bring the value to [5,11). As the result is a double value casting it to an int will bring the value to [5,10] since casting to int will truncate the decimal value rather than round the number.

The above code will generate integer random numbers in the interval [5,10] both inclusive. Note that the random number in the interval [0,1) is multipled with 6 so we will get numbers in the range [0,6). Adding five to the value will bring the value to [5,11). As the result is a double value casting it to an int will bring the value to [5,10] since casting to int will truncate the decimal value rather than round the number.