Does that mean it’s never going to be 1?

It says .random(): “This method returns a random number between 0 (inclusive) and 1 (exclusive)”. Does that mean it’s never going to be 1? why?

No, it is never going to be 1. The why is simple, that would give two possible integers, which would need a quantum computer to deal with (just kidding). We cannot have more than 1 integer returned from the method, so the generated value can never equal 1.

1 Like

Thanks. Why can’t we have more than 1 integer returned?

1 Like

That is a good question, if none of what I explained earlier made any sense. It would mean we would already have a way to generate both 0 and 1 with the same function. We could hash out the integers and have a boolean generator. But lo, to do that we have to write our own function…

const choice = a => a[Math.floor(Math.random() * a.length + Math.min(...a))]
console.log(choice([0, 1]))           //  0 or 1
console.log(choice([1,2,3,4,5,6]))    //  1 .. 6

If we have to go that far, then we may as well go the distance.

const choice = a => a[Math.floor(Math.random() * a.length + (Math.min(...a) || 0))]
console.log(choice(['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']))
// indigo  (or one of red .. violet)

The point being made is that we need some level of predictability and as soon as that breaks down, the logic breaks down. With this function (random) we know with certainty that only 1 integer can result, and that is zero. On that certainty we can now build a program.

Zero is included in the solution set. It is not a limit, but a fixed value. 1 is not included in the solution since it is the limit.

lim(x) | x -> 1 // x approaches 1 from the left but never reaches

And it follows we are in the positive domain.

1 Like

can you explain why the second example you wrote provides “some level of predictability”? It seems arbitrarily different from the first one.

Also, I don’t understand your previous answer to the same post…

Why is 0 considered the integer that is included versus 1? What gives the number 0 the priority to be the one that is included in the bound? Is it because that is what the method is specifically and is there another Method that does that? I am trying to find consistency in the logic.

The slight difference is to account for the return from Math.min() when given an array of strings. In that case, the return is NaN, which is not a valid index.

NaN || 0    =>  0

Admittedly, on reconsideration, the spread is superfluous unless one can jog my mind as to why it was used. Jogging…

Okay, got it. Math.min() takes a comma separated sequence, not an array. The spread creates that sequence from the array.

The added logic was intended to account for when the minimum plus the floored random product results in an index that is out of range. However, it was flawed due to an omission.

const choice = a => {
  const x = Math.floor(Math.random() * a.length + (Math.min(...a) || 0))
  return a[x] || a[0]

Bear in mind that this is rabbit hole code not meant as production but experimental. It is meant to be taken apart and played with. Especially, it needs to run several times with the same inputs to give up its ghosts. It was only by restudying it that I found the latest one. Thanks for bringing this back to the surface for further examination.

But any number it returns is a decimal and not a “comma separated sequence”… Nor an “array”.

With all due respect, this answer assumes I have the context or intuition for that added information.

What i understood was that it’s because it’s related to some other method called math.min(). Is this correct so far?

Correct. Proof I did not fully understand your question, and assumed wherewithal of the code. If you are just getting started then this question should be tabled for later.

Understood, but we ask the question because it is at the beginning of our tutorial.

That doesn’t feel correct … That we get asked something but we aren’t given any of this information before hand to give us the ability to answer these. Am I missing something maybe?

Going back to the topic of two integers being returned by Math.random(). With 2 to the 64 possible floats, including zero, I would suggest this is a question to not set aside. Zero is in the solution set for Math.random(), a method that always returns a float. Think whether 1 should factor into this.

Now, let’s take another kick at your question. What information is causing you difficulty?

Is zero in the solution set of Math.random because it was defined that way or because it is fundamentally so?

I think my difficulty in understanding is because I am not fully understanding the principle or concept behind some of the Methods and not explaining whether they are defined that way or if they are resulting that way because of mathematical principle unrelated to coding.

I don’t think I have a specific question at this point… I think I’m just aware that I am memorizing things and not actually understanding the concept behind some of these things.

As far as fundamental is concerned, zero fits the float scenario, one does not. It is part of the definition of random(), as we know, but how fundamental it is one cannot say. It makes perfect sense though. The only principle I can see is the floating point arithmetic but that is beyond our scope, just now.

When we concentrate on what .random() returns, and accept that it will never be 1 (we can deduce this is by definition) but it may be zero. Once you become more familiar with the mechanics you’ll be able to build in your own predictability.

As for memorizing, nothing helps things sink into memory like lots of practice which generates muscle memory and intuition. Work with the code.

Math.random()  // generates a float somewhere in the interval [0, 1)

Math.floor(x)  // truncates a float

Thank you for the clarification.

I understand what you mean, but in comparison to mathematics, there was an understanding of the concept beneath what we were doing so the sense of directionality was present from the beginning. It seems that code can only be taught by putting these terms and commands spread out on a “table”, so to speak, and to practice memorizing them before contextualizing them altogether. Maybe code just doesn’t work like that.

Again thank you for taking the time to explain this to me.

1 Like

Everything on the table is rote learning. There is just too much to memorize. We need to own it. Think jigsaw puzzle. We study every piece, and find ways to position them using visual or shape clues.

Can we rote learn a jigsaw puzzle? No. We only get the complete picture once we’ve assembled all the pieces. Of course, the corners are the easiest ones to place, followed by edges. We’re already applying logic and reasoning.

Like any difficult puzzle, it can be a lot of work to get the picture to formulate. Learning code (any language) is not really about the terms used as it is about the concepts. What is promised by the language for a particular keyword or function?

Taking for example the above Math.random(), JavaScript promises us that the method will always return a float less than 1, equaling or greater than 0. It’s important we know and understand that promise if we are to implement this method within its usage parameters. Knowing what to expect from a function is half the learning. Successfully implementing it to match our expectations is the other half.

This all takes time. Keeping good notes will help, but what is most helpful is taking it in small bites. Go through the documentation on MDN and W3C, and ECMA when you get further along, and keep a note of each visit and what you looked for on that visit. Over time you will find yourself re-visiting docs you read before. Bingo. That is when you know you are starting to learn. When you are researching your own ideas and reviewing the tools you have chosen.

It is less about what you can memorize, and more about what you can experience. One lesson doesn’t offer a lot of experience, but if we review the narrative and study the makeup of the exercise and intuit how and why that lesson has value, and what that value is, that is learning. It takes time spent on every lesson. And regular review. The Learning Environment is always active so as long as we don’t click Next, we can Run the page as often as we like. That means we can use the lesson page as a sandbox. Play with everything you learn.

Thank you again for taking the time to type that.

and ah I’m glad that you say it is less memorization and more of what you experience, because I’ve been used to learning NOT through memorization but by understanding and applying something.

I completely understand and I am prepared to tackle this the same way I had successfully tackled physics (much easier than coding in my opinion) and math, which is by looking at different sources, different textbooks, videos, tutorials, etc. The one thing I have noticed with multpile coding tutoring institutions/literature, is that for the most part, they don’t tell us from the beginning that the understanding comes from assembling many separate pieces BEFORE we get a sense of the picture. It really helps to be told that simple fact because if we don’t understand something we can feel comfortable to keep going and that it will make better sense later. This is what is jarring when compared to math or physics where the concept is easily understood but it’s the mathematical process that we are trying to practice…but we can at least consider our learning of the process and the understanding of the concept as occurring simultaneously. It’s because of this that on the first few pages (of the 3 previous attempts spanning over 5 years) when i don’t understand something and I’ve read every word in the book up to that point, i tell myself, “no, this book says it’s for complete beginners so I don’t think that they would make this THIS difficult to understand…” and I could never progress past tutorials. I truly think that the success rate of people trying to learn code would increase dramatically if on the back of the books they wrote two sentences

  1. If you don’t understand something, practice it anyway, and come back to it. <— this prevents people from getting stuck and then stopping completely out of fear that they won’t be able to do anything after that.
  2. Programming uses some words in different ways, when you come across a word that does not feel grammatically correct, do a Google search for “Programming definition of ______” with the word that seems out of place and you will get more information. <----- this is huge. No book ever told me that the word “pass” had a specific definition. and it’s not just that word but many others that wouldn’t normally be an issue but all books and people assisting use these terms even when in the beginning of the tutorials and it took a while before i realized that terms were being used in different ways without being told so I didn’t realize until I had gotten stuck on something for a while.

Obviously I know that you are not the person who wrote the instructions I am just stating that to give more context as to what my difficulty was and why it was.

Thank you, i am continuing with the tutorials as I type.

1 Like