• Register
1 vote
29 views

Problem:

Hello mate

I am in deep trouble. I want to generate a random float between 0 and 1 in a java program. What is the procedure to generate a random float number? Please share your thoughts on this.

Thanks in advance.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

There is a built-in class in java name java.util.Random. The nextFloat() is a method of that class. It generates a random float between 0.0 (inclusive) to 1.0 (exclusive). Let’s use the class and its method to generates random float number in a java program

import java.util.Random;
public class MyClass {
   public static void main(String[] args) {
      Random obj = new Random();
      System.out.println(obj.nextFloat());
   }
}

The output I got

0.9972967

 

13 9 6
94,240 points

Related questions

0 votes
1 answer 11 views
11 views
How can I get this?
asked Dec 19, 2020 TeamScript 13.5k points
0 votes
1 answer 31 views
0 votes
1 answer 18 views
0 votes
1 answer 7 views
7 views
How can I do this?
asked Dec 19, 2020 TeamScript 13.5k points
0 votes
1 answer 3 views
3 views
Problem: I want to generate Random Float numbers between Interval [0,1]. The generated Random values will be compared with an input from a file. The content of the File is a probability (float values) and hence they are in range[0,1]
asked Feb 18 Ali 4.4k points
0 votes
1 answer 29 views
29 views
Problem Hey, i just started learning C++ and i was trying to generate random integers but i was getting wrong.because i don’t know how to generate random numbers. Somebody can help me to write a code for generating a random number between 1 and 3. Try to provide a simpler way to answer me..
asked Dec 23, 2020 chris jordan 2.4k points
0 votes
1 answer 17 views
17 views
I want to generate some float number between 0 and 1 in C++. Can anyone tell me how to do it?
asked Dec 7, 2020 TeamScript 13.5k points
0 votes
1 answer 158 views
158 views
Problem: Hi there! I got an assignment in which I have to generate a game that works like ludo. This would be a two-player game. My program will generate a random number between 1 and 6 whenever a player presses Enter key and adds all the numbers of a user unless the ... excedes 100. I do not know how to generate random numbers between 1 and 6. I would be glad if someone could help me. Thanks!
asked Nov 10, 2020 Code Learner 5.8k points
0 votes
1 answer 7 views
7 views
Problem: Get random number between 0 and 1 in java In this post, we will see how to get random number between 0 to 1 in java. We have already seen random number generator in java. Math.random method returns double value between o(inclusive) to 1(exclusive).
asked Dec 3, 2020 Mashhoodch 9.1k points
0 votes
1 answer 5 views
5 views
Problem: java generate random number between 1 and 100
asked 3 days ago Hira 2.2k points