It is necessary to generate a number consisting of 16 digits, necessarily without a significant one. Just int here does not fit, but BigInteger is too big. The numbers must be unique, at least in the range of 200,000 copies.

long min = 1000000000000000; long max = 9999999999999999; Random rand = new Random(); long id = rand.Next((Int32)(min >> 32), (Int32)(max >> 32)); id = (id << 32); id = id | (long)rand.Next((Int32)min, (Int32)max); return id; 

in this case, and not always by 16 characters, and there are repetitions.

  • 2
    What is the question? Something is not clear to me what exactly is failing. - Vadim Ovchinnikov
  • 3
    If uniqueness is required - Hashset and save all generated numbers there, when generating a new one - check. - Andrei NOP
  • 3
    The fact that Random’s grain is Int32 is subtly hinting that normal pseudo-random 64 bits cannot be achieved from it. If you are not confused by the holes and unevenness, you can use the formula @ Andrew. In your own formula, the border shifts are superfluous, look in the debugger that you are transmitting to Next. Well, with binary concatenation of numbers, no one promises decimal concatenation, of course. - Athari
  • five
    @VladFinni GUID? Twice as many bits, but definitely unique. - Athari
  • 2
    @Andrey Entropy for a pseudo-random int and for a pseudo-random long, obtained by concatenating two consecutive pseudo-random ints, the same - the second half of a long can be calculated from the first half. This is the question of the meaning of such an operation. The internal state of Random is defined by 32 bits, that is, it is purely mathematically impossible to get any 64-bit number. The segment of permissible values ​​consists of unreachable holes slightly less than completely. Random.Next gives a uniform distribution, but no one guarantees what distribution the combined values ​​will have. - Athari

1 answer 1

The variant born in comments, I will take out in the form of the answer:

 class MyRandom { Random rand1 = new Random(Guid.NewGuid().GetHashCode()); Random rand2 = new Random(Guid.NewGuid().GetHashCode()); const int size = 200000; HashSet<long> values = new HashSet<long>(); public long GetNext() { long next; do next = rand1.Next(10000000, 100000000) * 100000000L + rand2.Next(100000000); while (values.Contains(next)); values.Add(next); if (values.Count == size) values.Clear(); return next; } }