Mr A.
asked 08/19/21Java 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.
- Generate a random integer "i" such that "i" is in the following range: 10 <= i < 20; 10 <= i <= 50.
- Write an expression that returns '0' or '1' randomly using Math.random()?
Please show your work.
1 Expert Answer

Patrick B. answered 08/19/21
Math and computer tutor/teacher
class RandomInts
{
public long RandomLongBetween( long minVal, long maxVal)
{
return(
(long) (Math.floor(Math.random()*(maxVal-minVal)))+minVal
);
}
public boolean RandomBoolean()
{
return(
(Math.random()>0.5)
);
}
public static void main (String args[])
{
RandomInts myRandomRNG = new RandomInts();
int N1=10;
int N2=25;
System.out.println(" some integers between 10 and 20..... ");
for (int iLoop=0; iLoop<N1; iLoop++)
{
System.out.print(myRandomRNG.RandomLongBetween(10,20) + " ");
}
System.out.println("\n----------------------------------------\n");
System.out.println(" some integers between 10 and 50..... ");
for (int iLoop=0; iLoop<N2; iLoop++)
{
System.out.print(myRandomRNG.RandomLongBetween(10,50) + " ");
}
System.out.println("\n-----------------------------------------\n");
System.out.println(" some random booleans ");
for (int iLoop=0; iLoop<10; iLoop++)
{
System.out.print(myRandomRNG.RandomBoolean() + " ");
}
}
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).08/19/21

Patrick B.
I am using it.... look in the method GenerateRandomLongBetween.. you will see I am using the exact same formula.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.08/21/21
Mr A.
This is more of a math problem than a code problem.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);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.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 false08/21/21
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
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.08/21/21