Code Monkey home page Code Monkey logo

Comments (2)

reepush avatar reepush commented on May 24, 2024 1

@zeh Thanks for your reply! I was thinking about generating valid ids in advance, but solution with checking duplicates works good enough for me. Would be nice to have unique ids out of the box as I don't see any other solutions for this problem.

from prando.

zeh avatar zeh commented on May 24, 2024

Hi @reepush,

Sorry about losing users. :(

The problem in that case is that it's constraining the range of valid numbers to 899,999 integers. Even though the algorithm can generate 536,870,912 unique integers, constraining it like that will always create collisions, sooner or later. In fact, if you ran through the entire set of valid random values, any number would end up with about 596 collisions.

This is by design; the library does not attempt to generate unique lists, but rather to get a mostly random number. In this case, what I mentioned on the documentation for nextArrayItem() applies:

Note: keep in mind that while the returned item will be random enough, picking one item from the array at a time does not guarantee nor imply that a sequence of random non-repeating items will be picked.

In your case, you were selecting an int, but it's coming from a limited set, so it's a similar problem...

Checking for a duplicated it might work, but IMO it's a kind of a work around rather than actually solving the problem. It might make your code slow too.

I think better strategies in your case for the loop would be:

  1. weak: use the sequential index itself.
  2. not as weak: use the sequential index itself, but XOR it with another number so it looks pseudo-random.
  3. strong: create an array of all valid ids (100,000...999,999), shuffle the array elements, and then read them in sequence.

Again, sorry for losing users.

from prando.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.