Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • "(barring an extremely unlikely accident where Math.random() returns the same value twice in a row)." Two things here: 1. it doesn't have to be in a row. If you get "a", "b", "a" as keys, that still clashes, without being in a row. 2. Depending on the range of values, it's not that unlikely due to the birthday paradox. With a range of 15 possible random values and calculating a random value 3 times, the chance for a collision is about 20%. If we go with 4 calculations, that's 35% chance. Commented Feb 5 at 13:02
  • @VLAZ True - I was considering the strict case here where there are only 2 properties in the object. No doubt a scenario will arise where there are more. And the problem is exacerbated by rounding the values, greatly decreasing the possible outcomes that are passed to the object - Math.random generates a far larger possible number of values by itself. However, the OP did indicate that this isn't the real function, so probably it's a moot point for them in their actual work Commented Feb 5 at 13:27
  • @VLAZ In my actual use case, there absolutely can and will be far more than two items. Definitely hundreds, possibly thousands; on an off chance, tens of thousands, though at that point we'd likely be looking to branch some of our data into something like SQL. They're all being handled on a procedural basis, so you don't really need to keep track of all that - but in the event you need to trace the stack down for bugfixing, or perhaps on a rare case access a part of the datastructure directly to modify it (think useRef) you'll be able to. Commented Feb 5 at 13:32
  • 1
    Instead of random numbers, why not just increment a counter? Commented Feb 5 at 16:48
  • @Barmar That might be a question to ask the OP, not me :-) Commented Feb 5 at 18:24