440,117 Members | 2,142 Online
Need help? Post your question and get tips & solutions from a community of 440,117 IT Pros & Developers. It's quick & easy.

# Random Number Generation

 P: n/a I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0; i
8 Replies

 P: n/a Anil Gupte wrote: I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0; i

 P: n/a Anil Gupte wrote: I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0; i

 P: n/a I thought Random strings should be unique. I understand that this is a pseudo random number being generated, but as I understand it, the seed is generated from the system clock and so there is at least a good chance that they are unique. In fact, I realize that even in a perfect random number generator there is a chance that the same number will keep appearing, but the probability of that is fairly low. Anyway, thanx for that function in your previous message, I will try it. -- Anil Gupte www.keeninc.net www.icinema.com "Arne Vajhřj" I had someone write a random number generator in C# (I am more of a VBprogrammer) and they came up with the following:public string GetRand(int count){string number = "";for (int i=0; i

 P: n/a Anil Gupte wrote: I thought Random strings should be unique. No, the strings are not guaranteed to be unique. If they were, they would not be random. Each random number has the same probability for any number in the range, it won't have lower probability for numbers that has been used before. If you mean that each random values has an even probability in the range, that's true, but that is not uniqueness. I understand that this is a pseudo random number being generated, but as I understand it, the seed is generated from the system clock and so there is at least a good chance that they are unique. In fact, I realize that even in a perfect random number generator there is a chance that the same number will keep appearing, but the probability of that is fairly low. The Random object is only seeded from the clock when it's created. The subsequent random numbers are seeded from the previous random number. If you create a new Random object for every random number you pick, it will be seeded from the system clock every time. If you do that too close in time, like in the loop of your method, the system clock haven't changed, and you get the same random number over and over. -- Göran Andersson _____ http://www.guffa.com Oct 21 '07 #5

 P: n/a On Sun, 21 Oct 2007 09:38:27 +0530, "Anil Gupte" I had someone write a random number generator in C# (I am more of a VBprogrammer) and they came up with the following:public string GetRand(int count){string number = ""; Better to use StringBuilder and append. >for (int i=0; inumber = number+Convert.ToString(Rnd.Next(0,9)); Rnd.Next(10) has the same effect. >}return number;}I want to return a string of digits where each each string is unique. The code you have does nothing to make sure that the string produced is unique. One way to avoid repeated strings is to use a different seed for each string, with sufficiently long strings there is a one-to-one relationship between strings and seeds. >Unfrotunately, this function is retrning strings like "11111111" and"44444444" and so on. Using new Random() initialises the RNG from the clock. Putting this in a tight loop means that the clock does not have time to tick over between calls, so all the digits generated are repeats. THe code need to move the new Random() outside the loop. >This means the seed is not getting initialized oneach interation but only on the first one. It is initialised on each loop, but because the clock has not changed it is being initialised to the same value and giving the same output. >Now in VB, I would have put theRandomize() satement inside the loop so a new seed is innitialized. Isthere an equivalent in C#? Is there a better way to do what we need?Thanx, Incorporating this into code, and using StringBuilder rather than creating a lot of immutable strings, I would write: public string GetRand(int seed, int count) { Random rand = new Random(seed); StringBuilder sb = new StringBuilder(count); for (int i = 0; i < count; ++i) { sb.Append((char)('0' + rand.Next(10))); } return sb.ToString(); } For long enough strings, each seed will give a unique string. If you never repeat a seed then you will never repeat a string. rossum Oct 21 '07 #6

 P: n/a Anil Gupte wrote: I thought Random strings should be unique. I understand that this is a pseudo random number being generated, but as I understand it, the seed is generated from the system clock and so there is at least a good chance that they are unique. In fact, I realize that even in a perfect random number generator there is a chance that the same number will keep appearing, but the probability of that is fairly low. If you generate 2 numbers with 5 digits this way the probability is 1/10000 that there will be a duplicate. If you generate 100 numbers with 5 digits this way the probability is about 40% that there will be a duplicate. If you google for uuid or guid you will find better approaches to ensure uniqueness (even though some of them include random generators). Arne Oct 21 '07 #7

 P: n/a Thanx! -- Anil Gupte www.keeninc.net www.icinema.com "rossum" >I had someone write a random number generator in C# (I am more of a VBprogrammer) and they came up with the following:public string GetRand(int count){string number = ""; Better to use StringBuilder and append. >>for (int i=0; i>number = number+Convert.ToString(Rnd.Next(0,9)); Rnd.Next(10) has the same effect. >>}return number;}I want to return a string of digits where each each string is unique. The code you have does nothing to make sure that the string produced is unique. One way to avoid repeated strings is to use a different seed for each string, with sufficiently long strings there is a one-to-one relationship between strings and seeds. >>Unfrotunately, this function is retrning strings like "11111111" and"44444444" and so on. Using new Random() initialises the RNG from the clock. Putting this in a tight loop means that the clock does not have time to tick over between calls, so all the digits generated are repeats. THe code need to move the new Random() outside the loop. >>This means the seed is not getting initialized oneach interation but only on the first one. It is initialised on each loop, but because the clock has not changed it is being initialised to the same value and giving the same output. >>Now in VB, I would have put theRandomize() satement inside the loop so a new seed is innitialized. Isthere an equivalent in C#? Is there a better way to do what we need?Thanx, Incorporating this into code, and using StringBuilder rather than creating a lot of immutable strings, I would write: public string GetRand(int seed, int count) { Random rand = new Random(seed); StringBuilder sb = new StringBuilder(count); for (int i = 0; i < count; ++i) { sb.Append((char)('0' + rand.Next(10))); } return sb.ToString(); } For long enough strings, each seed will give a unique string. If you never repeat a seed then you will never repeat a string. rossum Oct 21 '07 #8

 P: n/a Thanx! -- Anil Gupte www.keeninc.net www.icinema.com "Göran Andersson" I thought Random strings should be unique. No, the strings are not guaranteed to be unique. If they were, they would not be random. Each random number has the same probability for any number in the range, it won't have lower probability for numbers that has been used before. If you mean that each random values has an even probability in the range, that's true, but that is not uniqueness. >I understand that this is a pseudo random number being generated, but asI understand it, the seed is generated from the system clock and so thereis at least a good chance that they are unique. In fact, I realize thateven in a perfect random number generator there is a chance that the samenumber will keep appearing, but the probability of that is fairly low. The Random object is only seeded from the clock when it's created. The subsequent random numbers are seeded from the previous random number. If you create a new Random object for every random number you pick, it will be seeded from the system clock every time. If you do that too close in time, like in the loop of your method, the system clock haven't changed, and you get the same random number over and over. -- Göran Andersson _____ http://www.guffa.com Oct 21 '07 #9

### This discussion thread is closed

Replies have been disabled for this discussion.