0

How to make a random number generator in Java?

Can anyone help me? I need to learn how to make a random number generator without using any built-in functions.

15th Jul 2022, 11:23 AM
Talha Malik
5 Réponses
+ 2
You can use Math.random(); return a fractional number between 0,1 You can use Random class instance and it's methods.. Random r = new Random(); r.nextInt( 50) ; within range 0,50 (edit) It has many other methods... edit: Talha Malik https://www.javatpoint.com/how-to-generate-random-number-in-java
15th Jul 2022, 11:29 AM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 I don't think nextInt() or any other method in Random can take two arguments. The range is probably between 0- to any number. nextInt(50);
15th Jul 2022, 11:48 AM
Bhaveshsingh Pawar
Bhaveshsingh Pawar - avatar
+ 1
ɴᴜʟʟ Yes. I may confused. I think it's in python. Iam Not remember clearly so i added relevant link which has clear details.. Talha Malik nextInt() can used to generate between range as random.nextInt(max - min) + min; //min = 1, max = 50 so returns a number between 1,50 ThreadLocalRandom library has same 2 argument method, direclty we can use.. Edit: Thanks for cache point.. edited
15th Jul 2022, 2:37 PM
Jayakrishna 🇮🇳
0
David (DonDejvo) randInt is not a function in Java
16th Jul 2022, 4:08 PM
Bhaveshsingh Pawar
Bhaveshsingh Pawar - avatar