Question : Random numbers

hey, i need to generate random number, but it mst not be duplicated more than once

e.g. 0001,0002,0003

i cant not have 0001 again it has to be unquie


it does not have to go in sequence

the lenght can be 12 chars long


in c# code Plz

Answer : Random numbers

>> Dhaest: The request is for non-repeating random numbers. How will your scheme provide that?

This is done through the hashtable !

I enhanced it to make sure you can get 12 digits back
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
        private void Form1_Load(object sender, EventArgs e)
        {
            Hashtable ht = new Hashtable();
            Random rm = new Random();
            int RmNum = 10;
            for (int i = 0; ht.Count < RmNum; i++)
            {
                Int64 nValue = GetNextInt64(1,999999999999);
                if (!ht.ContainsValue(nValue) && nValue != 0)
                {
                    ht.Add(nValue, nValue);
                    Console.WriteLine(nValue.ToString());
                }
            }
        }
        
        public long GetNextInt64(long low, long hi)
        {
            RNGCryptoServiceProvider _rng = new RNGCryptoServiceProvider();
            if (low >= hi)
                throw new ArgumentException("low must be < hi");
            byte[] buf = new byte[8];
            double num;

            //Generate a random double
            _rng.GetBytes(buf);
            num = Math.Abs(BitConverter.ToDouble(buf, 0));

            //We only use the decimal portion
            num = num - Math.Truncate(num);

            //Return a number within range
            return (long)(num * ((double)hi - (double)low) + low);
        }
Random Solutions  
 
programming4us programming4us