On Thursday, 20 January 2022 at 04:00:59 UTC, forkit wrote:
void makeUniqueIDs(ref uint[] arr, size_t sz)
{
  ...
}

arrg!

what was i thinking! ;-)

// ---
void makeUniqueIDs(ref uint[] arr, size_t sz)
{
    arr.reserve(sz);

    // id needs to be 9 digits, and needs to start with 999
    int[] a = iota(999_000_000, 1_000_000_000).array;
// above will contain 1_000_000 records that we can choose from.

    int i = 0;
    uint x;
    while(i != sz)
    {
       x = cast(uint)a.choice(rnd);

       // ensure every id added is unique.
       if (!arr.canFind(x))
       {
           arr ~= x;
           i++;
       }
       else
           i--;
    }
}


//------


Reply via email to