-1

I have the problem that when I want to have an id for my database I generate a random number for this the bad thing is that it is repeated.

public void Iniciar()
{
    Random r = new Random();
    int prevnum = -1;

    for (int loop = 0; loop < 10; loop++)
    {
        do
        {
            num = r.Next(1000,9999);
        } while (num == prevnum);

        prevnum = num;
    }
}
St. Pat
  • 733
  • 4
  • 12
  • 2
    Why not let the database generate the id? – Ron Beyer Apr 27 '18 at 18:38
  • 3
    That's because *random* does not mean *unique*. Randomness does not depend on previous state in any way. "1, 1, 1, 1, 1" is a perfectly valid sequence of 5 random numbers. What exactly are you trying to do? – David Apr 27 '18 at 18:40
  • Echo'ing @RonBeyer, it would be good to understand what you're trying to do, but on the face it seems you should let your database handle this use case - unless there is some reason you don't believe it should – Prescott Apr 27 '18 at 18:56
  • 1
    I'm guessing that maybe what you want is to shuffle some numbers, like creating a list of 1 to 100 and then shuffle them, like if they were cards. May that be the case? Otherwise, you need to either keep track of the previous numbers (not something nice to do) or use GUIDs, which are unique, but they are not numbers. – Andrew Apr 27 '18 at 19:16
  • Possible duplicate of [how to create unique id for each user in asp.net C#](https://stackoverflow.com/questions/19287270/how-to-create-unique-id-for-each-user-in-asp-net-c-sharp) – Marquizzo Apr 27 '18 at 22:36

2 Answers2

1

If you are simply looking for unique IDs (and you don't require integers), you can use a UUID:

String id = Guid.NewGuid().ToString()

If you need a unique integer ID, you are better off using your database to generate them (such as mysql's AUTO_INCREMENT)

retupmoca
  • 272
  • 1
  • 6
0

Simply: You cant, directly.

System.Random can generate same number over and over again. If you still want to get 'unique random' numbers, you need to store generated numbers in a list, then check if newly generated number is in list.

using System;
using System.Collections.Generic;

class Program
{
    public static int Main(string[] args)
    {
        var numbers = new List<int>();
        Random rnd = new Random();
        for (int n = 0; n < 20; n++)
        {
            int num = rnd.Next(1000, 9999);
            for (int i = 0; i < numbers.Count; i++)
            {
                if (num == numbers[i])
                {
                    while (num == numbers[i])
                        num = rnd.Next(1000, 9999);
                    i = -1;
                }
            }
            numbers.Add(num);
        }
        // Uncomment if you want sorting
        // numbers.Sort();
        // Print numbers so we can check that every entry is unique
        for (int i = 0; i < numbers.Count; i++)
            Console.WriteLine("Id: {0}", numbers[i]);
        return 0;
    }
}

This code will try to generate random numbers if same number is generated before, then restart to check number with list. Pretty inefficient and does not works quite well with 'edge' numbers. (For example you try to generate a 20 entry list with only 19 random numbers)

Yahya Gedik
  • 97
  • 1
  • 12