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.
5 Answers
+ 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
+ 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);
+ 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
0
David (DonDejvo) randInt is not a function in Java