How does the getting random numbers formula work?

js

#1
static generatePassword 
 {
   return Math.floor(Math.random() * 10000);
}

I am working on the following exercise, have been introduced to thjis concept in multiple context, and am just missing a small piece.

How do I get this to run?

Why does the above not work?

Also, why cant “getting a random nu,ber bwetween two values” as defined by MDN
not work?

For your convenience, here is the formula they used:

function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;

How could I make getRandomArbitrary work for this problem?

I understand max is 10001, because it is not inclusive, and min is 0, but outside of the bracket, is that min also zero?

CA exercise: classes -
static methods


Is my hypothesis of how I add a property to an object correct?
#2

Here is the full code. However, it’s not all relevant but gives context.

class HospitalEmployee {
  constructor(name) {
    this._name = name;
    this._remainingVacationDays = 20;
  }
  
  get name() {
    return this._name;
  }
  
  get remainingVacationDays() {
    return this._remainingVacationDays;
  }
  
  takeVacationDays(daysOff) {
    this._remainingVacationDays -= daysOff;
  }
  
  static generatePassword() {
    return Math.floor(Math.random() * 10000);
  }
}

class Nurse extends HospitalEmployee {
  constructor(name, certifications) {
    super(name);
    this._certifications = certifications;
  } 
  
  get certifications() {
    return this._certifications;
  }
  
  addCertification(newCertification) {
    this.certifications.push(newCertification);
  }
}

const nurseOlynyk = new Nurse('Olynyk', ['Trauma','Pediatrics']);
nurseOlynyk.takeVacationDays(5);
console.log(nurseOlynyk.remainingVacationDays);
nurseOlynyk.addCertification('Genetics');
console.log(nurseOlynyk.certifications);

#3

Hi @jacquesk
Read about static methods here
This will show you how to to use them.
You can call static methods in other methods in the class but they cannot be called on the objects themselves.


#4

That random number generation between two arbitrarily supplied numbers works.

.
.
Let me explain.

You can generate a number from 0 to a specific number by

Math.round(Math.random() * <the_specific_number>) 

this will give you an integer, not a decimal number as we are rounding that off

When you subtract min from max and then multiply the difference to Math.random() you get a decimal number between 0 and the difference.
Then when you add it to the min variable you will make that decimal number greater than min and less than max.

And I don’t know any situation where it won’t work.

Hope this is going to help you.


#5

Please remember to post a link to the exact exercise you are on. It makes it immensely simpler to address your question.

Will this method work in one of the classes?

set password () {
    return generatePassword();

It might mean adding a property to the constructor:

this._password = password;

With a little testing we can sort this out.


#6

Would this formula work too?
See below.

static generatePassword() { return Math.round(Math.random() * 10000 - 0) + 0;
Above, the max is 1000 and the min is 0 - these ar einside the parentheses.
What does the + 0 mean?

This technique is used on MDN:
click here

P.S. I know technically the output is the same in this case but I’m trying to learn the technique. Is this technique I suggest simply not appropriate?


#7

In this case the actual exercise does not matter because this particular formula is not dependent on the rest of the data. I was working on static methods but specifically using math.random


#8

Is this the correct syntax?


#9

It is correct.
But there is no need to add or subtract 0 as the output will be same without them.
(when you wish to generate a random number b/w ) and another number)

It is the appropriate technique to generate a random number.
:slight_smile:


#10

How would you do it for an integer between 15 and 35?


#11

When we want a random number that divides a range into equal intervals, we indicate that in the multiplier

Math.random()  =>  a float

let x = Math.random()

x => { 0 <= x < 1 | x is Real }

let x = Math.random() * 10

x => { 0 <= x < 10 | x is Real }

We still don’t have intervals, though; not until we floor it.

let x = Math.floor(Math.random() * 10)

x => { 0 <= x < 10 | x is Integer }

x => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

As probability goes, the probability that x will be less than 10 is 1. The probability that x is less than 0 is 0. There is a probability of 0.1 that x is one of the above.

When we round, that probability changes to 10 / 11 that it is less than 10 since rounding allows the outcome to be equal to 10.

let x = Math.round(Math.random() * 10)

x => { 0 <= x <= 10 | x is Integer }

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

It is important that this distinction is clear. Rounding is a usage case that must take into account the upper limit.

We shift the range by using an offset, such as 1 to remove the zero and to include the upper limit when flooring.

let x = Math.floor(Math.random() * 10) + 1

x => { 0 < x <= 10 | x is Integer }

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

If we use rounding, then,

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

floor is always rounded down to the nearest integer.

round is only rounded down is Math.random() < 0.5, other it rounds up to the nearest integer.

Rounding is better suited to working with floats that are near approximations, such as currency transactions.

Math.round(7.49)  =>  7
Math.round(7.50)  =>  8

If working on a probabilistic scenario, use floor.

An integer between 15 and 35 inclusive would look like,

Math.floor(Math.random() * (35 - 15)) + 15 + 1

#12
return math.round(Math.random() * (35-15) +15

#13

That will exclude 35. Mind, adding 1 as above will exclude 15 so that needs to be thought out.


#14

what is the

for? What is it’s role?


#15

Using round() in your example will actually include 35, d’uh!. I used floor() but it does not work exactly as originally thought.

0 + 15 = 15

0 + 34 = 34

In the last case we need one more offset, 1.

The floor example actually works correctly if we fudge the numbers, slightly…

Math.floor(Math.random() * (36 - 15)) + 15

Now it will include both 15 and 35 as your rounding example does.

Bottom line, we need to test our ideas to make sure we are getting the right fit and can predict all possible outcomes. Running a loop over several hundred iterations will help to prove out one’s theory.

If probability is the goal, such as 10 equal chances of winning, then use floor; but, if a desired range includes the upper bound, then use round. There is no one perfect fit without careful consideration.


#16

So you said this is includes 15 anbd goes up by one? I need to read over your stuff again, too, though.


#17

Yes, that method will include both 15 and 35 in the possible outcomes and will be integers. Of note, rounding results in 21 possible outcomes, where flooring only results in 20, hence above I had to fudge the upper value so 35 got included. Be sure you are aware of the full set of possible outcomes, is my main point.


#18

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.