• Register
1 vote
38 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,260 points

Related questions

0 votes
1 answer 33 views
0 votes
1 answer 35 views
35 views
How can I get this?
asked Dec 19, 2020 TeamScript 13.5k points
0 votes
1 answer 57 views
0 votes
1 answer 5 views
5 views
Problem Can anyone help me out here! What's the definition of float?
asked Mar 11 neeraj 10.5k points
0 votes
1 answer 38 views
38 views
Problem: While strings can be multiplied by integers to create a repeating sequence, strings cannot be multiplied by floats. Otherwise, Java returns an error.I was going to talk about the “typeerror: can t multiply sequence by non-int of type ‘float’” error and why it is raised. We’ll walk through an example scenario with this error present so that how can i solve it.
asked Dec 2, 2020 Mashhoodch 13k points
0 votes
1 answer 11 views
11 views
How can I do this?
asked Dec 19, 2020 TeamScript 13.5k points
1 vote
1 answer 9 views
9 views
Problem: Any ne who canhelp me with the solution please? TypeError: unsupported operand type(s) for ^: 'float' and 'float'.
asked Mar 30 PkGuy 23.5k points
0 votes
1 answer 40 views
40 views
Problem: How to I Solve this: unsupported operand type(s) for ^: 'float' and 'int' HELP! I need help understanding this: unsupported operand type(s) for ^: 'float' and 'int' Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 3 Mashhoodch 13k points
0 votes
1 answer 22 views
22 views
Problem: I wrote a simple program which approximates the evaluation of a definite integral using numerical integration. However, I am stumped when it comes to why I am getting the error in the title. Keep in mind that I haven't touched python in one and a half years so it might be something incredibly obvious that I'm missing, however I'd still be grateful if you could help me
asked Feb 23 Mashhoodch 13k points
0 votes
1 answer 41 views
41 views
Problem: Hi there! I am a new programmer, and I have started programming in Python just recently. I am facing a little difficulty in getting used to Python syntax. I am trying to run the following code: pi = "3.14"; floating = float(input("Enter a floating-point ... to subtract a value from the value of pi and print the answer. Could anybody here please point out the mistake I am making? Thanks!
asked Jan 8 Code Learner 9.8k points