0

Generating random number in JAVA

How can i generate randome number in java in output file, by using random.rand?

26th Nov 2018, 10:11 AM
JazzyB
JazzyB - avatar
8 ответов
+ 11
JazzyB in above comment , loop will run infinite times , until time-limit exceeds //U can do like this for running desired number of times: int a =10; //for running 10 times while(a-- > 0){ //statement(s); } //btw U can use any loop , recursion , stream etc .
26th Nov 2018, 12:20 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 10
//using Random Class ● import Random class //import java.util.Random; ●create an object of that class & generate a random number from 1 to 99 /* Random R1 = new Random(); number1 = 1 + R1.nextInt(99); //1 is added because R1.nextInt(99) generates random number from 0 to 98 number 2= 1 + R1.nextInt(99); //here is sample code : import java.util.Random; public class Program{ public static void main(String[] args) { while(true){ int number = 1 + (new Random()).nextInt(99); //generating number from 1 to 99 (both included) System.out.println(number); } } }
26th Nov 2018, 12:01 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 2
do i have to use while loop?
26th Nov 2018, 10:54 AM
JazzyB
JazzyB - avatar
+ 1
between 1 to 99
26th Nov 2018, 10:13 AM
JazzyB
JazzyB - avatar
+ 1
like genuinly i have to generate points. x,y to an ouput file, can you please ealborate the algo?
26th Nov 2018, 10:56 AM
JazzyB
JazzyB - avatar
+ 1
and thank you so much for reply
26th Nov 2018, 10:56 AM
JazzyB
JazzyB - avatar
+ 1
thanks gaurav,
26th Nov 2018, 12:21 PM
JazzyB
JazzyB - avatar
0
appericiated Gaurav but how many time will this loop rotate?
26th Nov 2018, 12:14 PM
JazzyB
JazzyB - avatar