+ 5

what is the code for generating a random integer number between 1 and 6?

in java

6th May 2017, 11:24 PM
chris
chris - avatar
10 Respuestas
+ 14
import java.util.Random; Random rand = new Random( ) ; int number = rand.nextInt (6) +1; // 6 is maximum, 1 is minimum
6th May 2017, 11:58 PM
NimWing Yuan
NimWing Yuan - avatar
+ 11
# Chris: 👍👍👍
7th May 2017, 12:17 AM
NimWing Yuan
NimWing Yuan - avatar
+ 11
# Chris: range will be from 25 +0 till 25 + 33 👍👍👍
7th May 2017, 12:30 AM
NimWing Yuan
NimWing Yuan - avatar
+ 10
# Chris: 👍👍👍
7th May 2017, 12:37 AM
NimWing Yuan
NimWing Yuan - avatar
+ 7
Two ways: import java.util.Random; Random rand = new Random(); then use: (rand.nextInt(max-min)+1)+min so: int num = (rand.nextInt(6-1)+1)+1 Or use: Math.Random() to get a num from 0 to 1. So: int num = (int)((Math.random()*5)+1) 6 should be exclusive here
6th May 2017, 11:51 PM
Rrestoring faith
Rrestoring faith - avatar
+ 6
@Rrstoring faith @NimWing Yuan is this also correct to generate random integer between 1 and 6: (int) ( 1 + (Math.random() * 6) ) ;
7th May 2017, 12:14 AM
chris
chris - avatar
+ 6
yes
7th May 2017, 12:15 AM
Rrestoring faith
Rrestoring faith - avatar
+ 6
so this code will print 25 - 34 ? (int) ( 25 + (Math.random() * 34) ) ;
7th May 2017, 12:17 AM
chris
chris - avatar
+ 5
@ nimwing did you say my recent code that I posted was wrong ?
7th May 2017, 12:28 AM
chris
chris - avatar
+ 5
between 25 and 58 right ?
7th May 2017, 12:31 AM
chris
chris - avatar