true random number generation

Hi;

I need true random number generation between two positive integers (around 50000-60000)

Is there a short but effective random number generation method that i can use in my C# app?

Best regards.
LVL 12
jazzIIIloveAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

skijCommented:
This will select a number no smaller than 50000 and no greater than 60000:
Random rnd = new Random();
int RandomNumber = rnd.Next(50000,60000);

Open in new window

Scott CSenior EngineerCommented:
That is not a "True" random number generator.

That would be pseudo.

https://en.wikipedia.org/wiki/Random_number_generation

You need to incorporate atmospheric or cosmic background noise to get a true random number.

I don't expect any points, but you did ask for a true random number generator.
skijCommented:
It is true that in computer science, just like in all science, there is actually no such thing as random.

That is why I stated:
This will select a number no smaller than 50000 and no greater than 60000:
Even if you flip a coin, the results are not actually random.  They are influenced by physical laws.

You have the option of influencing the order of numbers generated by seeding the random generator.   If you wish to have the sequence of numbers returned influenced by the time and date, try this:
int seed =  DateTime.Now.Ticks & 0x0000FFFF;
Random rnd = new Random(seed);
int RandomNumber = rnd.Next(50000,60000);

Open in new window

Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Russ SuterSenior Software DeveloperCommented:
As stated above there's no "true" random number generator in any computer unless you include outside seed data like cosmic radiation.

However, .NET does have a random number generator that is considered cryptographically secure.

https://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider(v=vs.110).aspx

There's quite a handy tutorial here:

http://thinketg.com/how-to-generate-better-random-numbers-in-c-net-2/

The numbers generated by this method are about as truly random as you can get and they are considered cryptographically secure.

Here's a little code snippet I whipped up that should give you exactly the result you're looking for

            using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
            {
                    byte[] rngData = new byte[4];
                    rng.GetBytes(rngData);
                    int cryptoInt = BitConverter.ToInt32(rngData, 0);
                    int randomNumber = Math.Abs(cryptoInt) % 10000 + 50000;
            }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
As  has been stated in earlier post there is really no true random number generator. You can use the one defined in C# by the .Net Framework. You can use the one suggested by skij in the first post
int RandomNumber = rnd.Next(50000,60000);

Open in new window

but note the first parameter is inclusive but the second parameter is exclusive. So if you need values between  50000-60000 inclusive you will need to modify skij solution as follows.
int RandomNumber = rnd.Next(50000,60001);

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
What do you need "true random generation" for jazzIIIlove?  How will you be using this?...
jazzIIIloveAuthor Commented:
Hi;

I meant a random generation that doesn't repeat really frequently. The generator function will be called in small time frames.
Fernando SotoRetiredCommented:
You state, "doesn't repeat really frequently", you can make it not to repeat at all by implementing a sorted List and as you get the random number from the pseudo-random number generator check it to see if it is in the list and if it is not return it to the caller and insert it into the list.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
That doesn't really help.  The standard Random class will probably work fine; have you tried it?  Just make sure that you instantiate it ONCE, at class level (not inside a method), and reuse that same instance over and over.
skijCommented:
You could use the code below to see an example of the numbers generated by the standard random class:
Random rnd = new Random();
int i = 0;
while (i++ < 1000) 
{
  Console.WriteLine("Random Number: {0}", rnd.Next(50000,60001));
}

Open in new window

Russ SuterSenior Software DeveloperCommented:
Random that doesn't repeat frequently? When you place constraints on a random number generator you're going to end up with a less random result. In an ideal situation each subsequent number has the exact same mathematical chance of coming up every time. That means in your number space you have a 1 in 10000 chance of a number appearing on any given try. You have the exact same chance, statistically, of getting the same number each time.

I'm getting the impression that you're not looking for a truly random number but one that is, instead, unique within the range for a given time slice. In that case you can either implement Fernando's idea or check the random number against previous random values and request a new random value if there's a collision. The latter method isn't great since the performance will get exponentially worse as you call it repeatedly.
käµfm³d 👽Commented:
A GUID may work here also. Although, my understanding is that they are somewhat expensive to create, so this business about "will be called in small time frames" may be of concern. You could certainly run tests to confirm whether or not it works. Just keep in mind that a GUID is a 128-bit number, so maybe that would be larger than what you need.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.