## c# - How do I generate a random int number? - Stack Overflow

Jul 26, · Hi, This may seem a very basic question, but I am having difficulty getting it to work. Could some one please tell me how I can generate a random number, between two given numbers, in C# please. · Use Random class. You could experiment a little, don't you think? And please use the MSDN documentation search. Try writing word "Random" to the search. NextDouble generates a random number between [, ] You multiply this random number by your maxLongValue. You snovwes.gq that result so you can get the chance to get maxLongValue anyway (eg: simulate you got from the NextDouble). You cast back to long. *“Random” class generates only PSEUDO random number and to generate SECURE random number we need to use “RNGCryptoServiceProvider” class. Random class takes seed values from your CPU clock which is very much predictable. So in other words RANDOM class of C# generates pseudo random numbers, below is the code for the same.

## c# - Random number in long range, is this the way? - Stack Overflow

By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. NextDouble returns a double between 0 and 1. You then multiply that by the range you need to go into difference between maximum and minimum and then add that to the base minimum.

Real code should have random be a static member. This will save the cost of creating the random number generator, and will enable you to call GetRandomNumber very frequently. Since we are initializing a new RNG with every call, if you call quick enough that the system time doesn't change between calls the RNG will get seeded with the exact same timestamp, and generate the same stream of random numbers. Johnny5 suggested creating an extension method. Here's a more complete code example showing how you could do this:.

Note that you should not create lots of new Random objects in a loop because this will make it likely that you get the same value many times in a row. If you need lots of random numbers then create one instance of Random and re-use it.

The simplest approach would simply generate a random number between 0 and the difference of the two numbers. Then add the smaller of **c# get random number in range** two numbers to the result.

From MSDN :. The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of Random. By default, the parameterless constructor of the Random class uses the system clock to generate its seed value If you need a random number in the range [ double.

MinValue ; double. MaxValue ]. About generating the same random number if you call it in a loop a nifty solution is to declare the new Random object outside of the loop as a global variable. Notice that you have to declare your instance of the Random class outside of the GetRandomInt function if you are going to be running this in a loop. If your loop is sufficiently fast, the system clock time will not appear different to the randomizer and each new instance of the Random class would start off with the same seed and give you the same pseudo random number.

I'm a bit late to the party but I needed to implement a general solution and it turned out that none of the solutions can satisfy my needs. The accepted solution is good for small ranges; however, maximum - minimum can be infinity for big ranges. So a corrected version can be this version:. This generates random numbers nicely even between double. MinValue and double. But this introduces another "problem", which is nicely presented in this post : if we use such big ranges the values might seem too "unnatural".

For example, after generating 10, random doubles between 0 and double. MaxValue all of the values were between 2. Here are the sorted results of generating 10, random double numbers between 0 and Double. MaxValue with both strategies. The results are displayed with using logarithmic scale:. Though the linear random values seem to be wrong at first glance the statistics show that none of them are "better" than the other: even the linear strategy has an even distribution and the average difference between the values are pretty much the same with both strategies.

Playing with different ranges showed me that the linear strategy gets to be "sane" with range between 0 and ushort. MaxValue with a "reasonable" minimum value of These are the same results of both strategies displayed with different scales:.

Recently I made my libraries open source, feel free to see the RandomExtensions. NextDouble method with the complete validation. Learn more. **C# get random number in range** 10 Free. Asked 10 years, 2 months ago. Active 1 month ago, *c# get random number in range*. Viewed k times. Is it possible to generate a random number between 2 doubles? VansFannel Michael Michael Keep in mind that since Random. NextDouble never returns 1. I suggest to put as a seed new Random int DateTime.

Ticks this should make it "more random" even in quick loops. This does not work if min value is double. MinValue and max value is double. Any suggestions on how to handle this use case? NextDouble 1. Mark Byers Mark Byers k gold badges silver badges bronze badges. Greg D Greg D Leniel Maccaferri Leniel Maccaferri Malcolm Malcolm Whow, a magic randomizer var has apeared!!

What if one of the values is negative? Chris Susie Chris Susie 19 1 1 bronze badge. I think the Math. Abs is redundant. MaxValue - double. ToDouble bytes, 0 ; if! Good point, **c# get random number in range**, but this proposition results in skewed distribution like in second example here stackoverflow.

Ajibola Ajibola 1, 15 15 silver badges 28 28 bronze badges. This is better suited as a comment, as it doesn't even attempt to answer OP's actual question, *c# get random number in range*. Min Math. Abs minValue**c# get random number in range**, Math. Max Math. Abs maxValue ; int sign; if! The results are displayed with using logarithmic scale: Though the linear random values seem to be wrong at first glance the statistics show that none of them are "better" than the other: even the linear strategy has an even distribution and the average difference *c# get random number in range* the values are pretty much the same with both strategies.

These are the same results of both strategies displayed with different scales: Edit: Recently I made my libraries open source, feel free to see the RandomExtensions. Leniel Maccaferri Mitesh Savani Mitesh Savani 1 1 1 bronze badge. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.

Post as a guest Name. Email Required, but never shown. Featured on Meta. Unicorn Meta Zoo 8: What does leadership look like in our communities? Visit chat. Linked Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.

### How do you create a random number in C#

Jul 26, · Hi, This may seem a very basic question, but I am having difficulty getting it to work. Could some one please tell me how I can generate a random number, between two given numbers, in C# please. · Use Random class. You could experiment a little, don't you think? And please use the MSDN documentation search. Try writing word "Random" to the search. NextDouble generates a random number between [, ] You multiply this random number by your maxLongValue. You snovwes.gq that result so you can get the chance to get maxLongValue anyway (eg: simulate you got from the NextDouble). You cast back to long. This is what I use to get distinct random numbers in a given range: var random = new Random(); int take = 4; int rangeMin = 0; int rangeMax = 10; var randomUniqueNumbers = snovwes.gq(0, snovwes.gque).Select(i => snovwes.gq(rangeMin, rangeMax)).Distinct().Take(snovwes.gq(take, rangeMax - rangeMin));.