Mr A.

asked • 08/19/21

Java Code: Generating random numbers with Math.random() within a certain range. Math.random() range: 0.0 <= x < 1.

Math.random() range: 0.0 <= x < 1.


  1. Generate a random integer "i" such that "i" is in the following range: 10 <= i < 20; 10 <= i <= 50.
  2. Write an expression that returns '0' or '1' randomly using Math.random()?

Please show your work.


Mr A.

Math.random() range: 0.0 <= x < 1. Generate a random integer "i" using *Math.random()* such that "i" is in the following range: 10 <= i < 20; 10 <= i <= 50. Write an expression using *Math.random()* that returns '0' or '1' randomly. Please show your work using *Math.random()* to show how you came up with these conclusions.
Report

08/21/21

1 Expert Answer

By:

Patrick B. answered • 08/19/21

Tutor
4.7 (31)

Math and computer tutor/teacher

Mr A.

The purpose is to use Math.random() method only. And its range ranges between 0 <= x < 1. For example, suppose that I want 'x' to be between 0 <= x < 10, then I would use the following code: (int)(Math.random() * 10).
Report

08/19/21

Patrick B.

I am using it.... look in the method GenerateRandomLongBetween.. you will see I am using the exact same formula.
Report

08/21/21

Mr A.

I can no longer edit or update my post, but I hope you can understand the necessary corrections in comments: Math.random() range: 0.0 <= x < 1. Generate a random integer "i" using *"*Math.random()*"* such that "i" is in the following range: 10 <= i < 20; 10 <= i <= 50. Write an expression using *"*Math.random()*"* that returns '0' or '1' randomly. Please show your work using *"*Math.random()*"* to show how you came up with these conclusions.
Report

08/21/21

Mr A.

This is more of a math problem than a code problem.
Report

08/21/21

Mr A.

For example: Generate an integer using Math.random() within range of 0 <= i < 20. Math.random() produces double values in between the range of 0.0 <= i < 1. Thus, if you utilize the following code, you will get values within the range of 0 <= i < 20: (int)(Math.random() * 20);
Report

08/21/21

Mr A.

How would you utilize the following code to produce results outside the range of Math.random(). When I say "show your work," I mean mathematically.
Report

08/21/21

Patrick B.

examine the function generateRandomLongBetween() it returns a random # between min and max values passed as paramters... the FORMULA for doing that is shown in the return statement of the function choose values for min,max, and math.random() test the calculations. ex. random # between 10 and 15, with math.random() returning 0.25 floor((0.25)*(15-10))+10= floor( (0.25 * 5) + 10 = floor(1.25) + 10 = 1+10 = 11 for random boolean, it greater than 0.5 then true, else false
Report

08/21/21

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.