 # Generate Random Fractions with JavaScript

## Solutions

Solution 1 (Click to Show/Hide)
``````function randomFraction() {
// Only change code below this line.
var result = 0;
// Math.random() can generate 0. We don't want to     return a 0,
// so keep generating random numbers until we get one     that isn't 0
while (result === 0) {
result = Math.random();
}

return result;
// Only change code above this line.
}
``````
5 Likes
``````function randomFraction() {

// Only change code below this line.

return 4; // chosen by fair dice roll.
// guaranteed to be random.

// Only change code above this line.
}``````
6 Likes

Is there anything inherently wrong with what I did?

function randomFraction() {
// Only change code below this line.
var x , a , b;
x= 0;
a = 0;
b = 0;
x += Math.random();
a += Math.random();
b = x/a;
return b;

// Only change code above this line.
}

3 Likes

function randomFraction() {

// Only change code below this line.

return Math.random();

// Only change code above this line.
}

10 Likes

This could generate a 0 no?

If Math.random generates a 0: x += Math.random === 0 which gives a return of 0 ??

This challenge is presenting me with the math.Random() function. math.Random() returns a number between 0 (inclusive) and 1 (exclusive). The challenge wants me to create a function using math.Random() that makes the 0 exclusive.

Can somebody give a hint please?

right,
should be:

function randomFraction() {

// Only change code below this line.

return 1 - Math.random();

// Only change code above this line.
}

1 Like

Done function randomFraction() {

// Only change code below this line.

return Math.random();

// Only change code above this line.
}

6 Likes

Hi, can you give me a lay-mans term on how it works, that is how does Math.random() work?

Math.random() function returns decimal values.
function randomFraction() {

// Only change code below this line
var aa = Math.random();
console.log(aa);
return aa;
// you can see output value within your challenge window, it will clarify the usage of function in detail when you check its output.
// Only change code above this line.
}

1 Like

My code works but I am unsure that it works for the right reasons, nobody else used the same concept to solve. I would love feedback.

function randomFraction() {

// Only change code below this line.
var num = Math.random();
if (Math.random(num) !== 0) {
return num;
}

return num;

// Only change code above this line.
}

Okay, so it pretty much still returns a decimal value in the console. Now is that because aa stores to what Math.random() is doing, which is to handle numeric values(more than 0 but less than 1). Am I correct in that assumption?

yes correct. This challenge is make campers familiar with the functionality of Math.random() function.

Math.random(); function that generates a random decimal number 0(inclusive). So it is not required to check using if condition because 0 is a integer number.
You can try reversing condition for better clarification
// Only change code below this line.
var num = Math.random();
if (Math.random(num) == 0) {
return num;
}
So extra code not required here.

1 Like