0

I'm trying to create a method that stores a unique random number to a class, and the way I am checking if it is unique is by looking through a list that contains an int, string and date. I'm just sort of stuck on how you would have it just search the ints of the list.

  • Possible duplicate: http://stackoverflow.com/questions/1011198/non-repetitive-random-number-c-sharp or http://stackoverflow.com/questions/5561742/generate-distinct-random-numbers-in-c-sharp – Sam Nov 16 '13 at 17:44
  • possible duplicate of [How to generate "random" but also "unique" numbers?](http://stackoverflow.com/questions/909983/how-to-generate-random-but-also-unique-numbers) – Kurubaran Nov 16 '13 at 17:44
  • no those can still create a number that is in the list, even if they are unique –  Nov 16 '13 at 18:01
  • What is the range of numbers you need? – Rob Lyndon Nov 16 '13 at 21:40

2 Answers2

0

You can use a guid, is based in your computer id and date. Is by definition a unique string every time.

use Guid.NewGuid()

Well if it is just numbers you can use a mathematical function like this one here then use the current date to limit our possible numbers and use the random functions to generate your unique number.

You can use in c# this DateTime.Ticks to get there.

Juan
  • 1,352
  • 13
  • 20
0

Use Math.Random Class. See here i m using this class to generate password. You can specify what are the data it should support. if you need integer only then you can specify (1 to 9 and 0). Here is one example.

    private string CreatePassword(int length)
    {
        string valid = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#$";
        string res = "";
        Random rnd = new Random();
        while (0 < length--)
            res += valid[rnd.Next(valid.Length)];
        return res;
    }

    public void InitializeData()
    {
        string password = CreatePassword(6);
    }
Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62