-1

I'm trying to get random number between 0 and Array.length. I have this:

getRandom() {
  const cars = Object.keys(this.index);
  const randomInt = Math.floor(Math.random() * cars.length);
  return cars[randomInt];
}

I ran this few times and found a 0 in one of the results from getRandom(). There is not key in my this.index object named 0.

Is my math function wrong?

UPDATE

After reading the comments, and I know the getRandom() is not wrong. I also have reset() function if you guys can look at it.

reset() {
  const cars = Object.keys(this.index);
  let i = cars.length;
  while (i--) {
    this.index[cars[i]] = 0;
  }
}

Is it possible I'm adding a new key 0 at this.index object?

cocacrave
  • 2,453
  • 4
  • 18
  • 30
  • 1
    The function is not wrong, but your expectations might be. What are valid random values? – str Aug 29 '16 at 09:28
  • 1
    `Math.random()` can return `0`. so this `Math.floor(Math.random() * cars.length);` can end up being `0` – Craicerjack Aug 29 '16 at 09:28
  • 1
    `cars` is an array. Unless it is empty, it has an entry at index 0. That entry (not the 0 itself) should be a valid key of `this.index`. – Thilo Aug 29 '16 at 09:28
  • 1
    `Object.keys` returns array, `cars[randomInt]` gonna return you `randomInt`'th element of array – Ivan Shmidt Aug 29 '16 at 09:28
  • 2
    Also, `Math.floor(0.49) `= 0 – Ivan Shmidt Aug 29 '16 at 09:29
  • 2
    You probably wanted `return this.index[cars[randomInt]]` on the last line. – georg Aug 29 '16 at 09:30
  • 1
    You can make use of this existing solution. http://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range And pass 0 and array.length as params. – Shankar Shastri Aug 29 '16 at 09:30
  • 1
    There's a bit of confusing information in your question.. do you want the function to return the random number, a random key from the `this.index` object or a random property from that object? – geekonaut Aug 29 '16 at 09:32
  • @geekonaut I'm trying to get a random keyname from object `this.index`. All I need is the keyname. – cocacrave Aug 29 '16 at 09:42

3 Answers3

1

I can't see an actual problem here.

Object.keys will turn your named keys into numbers (look here https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/keys), this numbers starts with 0.

So your function, as you wrote yourselv, will return an:

random number between 0 and Array.length

lippoliv
  • 727
  • 2
  • 11
  • 28
0

You can make use of this existing solution.

Generating random whole numbers in JavaScript in a specific range?

And pass 0 and array.length as params.

Community
  • 1
  • 1
Shankar Shastri
  • 1,134
  • 11
  • 18
-1

I wonder what you're seeing, but we're all lacking context here, so I made a little example to try the function to see the problem with my own eyes and it does not seem to have the problem you described.

Try it for yourself here - and maybe it's the data you're running the method on that is the problem?

geekonaut
  • 5,714
  • 2
  • 28
  • 30