+ 5
what is the code for generating a random integer number between 1 and 6?
in java
10 Respostas
+ 14
import java.util.Random;
Random rand = new Random( ) ;
int number = rand.nextInt (6) +1; // 6 is maximum, 1 is minimum
+ 11
# Chris: đđđ
+ 11
# Chris: range will be from 25 +0 till 25 + 33 đđđ
+ 10
# Chris: đđđ
+ 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
+ 6
@Rrstoring faith @NimWing Yuan
is this also correct to generate random integer between 1 and 6:
(int) ( 1 + (Math.random() * 6) ) ;
+ 6
yes
+ 6
so this code will print 25 - 34 ?
(int) ( 25 + (Math.random() * 34) ) ;
+ 5
@ nimwing did you say my recent code that I posted was wrong ?
+ 5
between 25 and 58 right ?