.NET Musings - Random Numbers in C#Wandering thoughts of a developer, architect, speaker, and trainer
http://www.skimedic.com/blog/
http://www.rssboard.org/rss-specificationBlogEngine.NET 3.2.0.3en-UShttp://www.skimedic.com/blog/opml.axdhttp://www.dotnetblogengine.net/syndication.axdPhil Japikse.NET Musings0.0000000.000000Anonymous on Random Numbers in C#The effect would be small with a small number like 10, since 10 divides into 2^31 214,748,364 times with a remainder of 8. That means there are 214,748,365 chances to get 1 through 8 and only 214,748,364 chances to get 9 or 10. Not very significant, but could easily become so if you were generating random numbers in the millions range, or a ton of random numbers, or needed high quality randomness.
http://www.skimedic.com/blog/post/2008/10/05/Random-Numbers-in-C.aspx#id_25cb37a9-e078-4adb-a2f3-9b8b438ad1d5
blog@skimedic.comhttp://www.skimedic.com/blog/post/2008/10/05/Random-Numbers-in-C.aspx#id_25cb37a9-e078-4adb-a2f3-9b8b438ad1d5Fri, 21 Aug 2009 23:43:08 -0400Anonymoushttp://www.skimedic.com/blog/pingback.axdhttp://www.skimedic.com/blog/post/2008/10/05/Random-Numbers-in-C.aspx#id_25cb37a9-e078-4adb-a2f3-9b8b438ad1d5http://www.skimedic.com/blog/post/2008/10/05/Random-Numbers-in-C.aspx#id_25cb37a9-e078-4adb-a2f3-9b8b438ad1d5#commenthttp://www.skimedic.com/blog/syndication.axd?post=25cb37a9-e078-4adb-a2f3-9b8b438ad1d5Philip Japikse, MCSD, MCDBA, CSM on Random Numbers in C#I'm not a mathemetician, so I tested my code the only way I see fit, with a unit test. I ran the generator 100,000 times to generate a number between 1 and 10. I then increment a counter for each result. The results I got the first run is this:<BR/>1:9914<BR/>2:9893<BR/>3:9831<BR/>4:10142<BR/>5:10016<BR/>6:10041<BR/>7:10073<BR/>8:10132<BR/>9:10078<BR/>10:9880<BR/><BR/>The second time is this:<BR/>1:10072<BR/>2:9870<BR/>3:10041<BR/>4:10032<BR/>5:10017<BR/>6:9971<BR/>7:9905<BR/>8:10115<BR/>9:9968<BR/>10:10009<BR/><BR/>I guess I just don't understand your argument, because these don't look skewed towards one end or the other.<BR/><BR/>I'm not sure of the use case you are trying to apply this to, but for line of business applications (where I earn my living), this is random enough (note that I said true"r" random numbers). If you need absolute true random numbers, it's going to be difficult to achieve with a computer.
http://www.skimedic.com/blog/post/2008/10/05/Random-Numbers-in-C.aspx#id_94a578e2-6355-4d99-ac47-7a25a8d322c9
blog@skimedic.comhttp://www.skimedic.com/blog/post/2008/10/05/Random-Numbers-in-C.aspx#id_94a578e2-6355-4d99-ac47-7a25a8d322c9Sat, 22 Aug 2009 04:43:08 -0400Philip Japikse, MCSD, MCDBA, CSMhttp://www.skimedic.com/blog/pingback.axdhttp://www.skimedic.com/blog/post/2008/10/05/Random-Numbers-in-C.aspx#id_94a578e2-6355-4d99-ac47-7a25a8d322c9http://www.skimedic.com/blog/post/2008/10/05/Random-Numbers-in-C.aspx#id_94a578e2-6355-4d99-ac47-7a25a8d322c9#commenthttp://www.skimedic.com/blog/syndication.axd?post=94a578e2-6355-4d99-ac47-7a25a8d322c9Thomas S. Trias on Random Numbers in C#If you want numbers between min and max inclusive, the calculation is ((result % (max - min + 1)) + min). This can be factored using Euler's theorems into multiple modulus operations, but since (max - min + 1) can be pre-calculated, this is the easiest method.<BR/><BR/>Unfortunately, if max + min (in your case - max - min + 1 in mine) doesn't evenly divide 2^31 (since you effectively zero out the sign bit), you are going to get some skew in your distribution towards the numbers near the beginning of the sequence. Also, taking the absolute value makes 0 / min less likely, since there is only one representation of zero.<BR/><BR/>Does anyone know of a better way to use the RNGCryptoServiceProvider to get a uniform distribution distribution of numbers besides throwing away some results? In the above case, the best I can come up with is to generate 2 bytes of random data, zero out bits 15 and 14 (since 16384 evenly divides 65536, and since the cryptographically secure random number should satisfy the next bit test, we still have a uniform distribution at that point), and then discard any results between 10001 and 16383 inclusive.<BR/><BR/>I thought there might be a way to reuse the leftover bits from the previous random calculation, but that still doesn't resolve the issue of what to do with the numbers between 10001 and 16383. If we generate 2560256 bytes worth of data, is there a good way to evenly distribute the results over 0 to 10000?
http://www.skimedic.com/blog/post/2008/10/05/Random-Numbers-in-C.aspx#id_3ba2127c-2726-4ec3-a000-10670c916a64
blog@skimedic.comhttp://www.skimedic.com/blog/post/2008/10/05/Random-Numbers-in-C.aspx#id_3ba2127c-2726-4ec3-a000-10670c916a64Sat, 22 Aug 2009 09:43:08 -0400Thomas S. Triashttp://www.skimedic.com/blog/pingback.axdhttp://www.skimedic.com/blog/post/2008/10/05/Random-Numbers-in-C.aspx#id_3ba2127c-2726-4ec3-a000-10670c916a64http://www.skimedic.com/blog/post/2008/10/05/Random-Numbers-in-C.aspx#id_3ba2127c-2726-4ec3-a000-10670c916a64#commenthttp://www.skimedic.com/blog/syndication.axd?post=3ba2127c-2726-4ec3-a000-10670c916a64