Random Number Generation for ulong (UInt64)

I have searched the internet extensively for an eloquent solution, but most of what try to pass themselves as "answers" are actually unreliable and use dodgy conversions.

So, when everything else seems to fail, I turn to ExpertsExchange ;)

Basically I am after using something along the lines of:

random.NextUInt64(min, max)

where min and max will be defined by me and of course must fall in the range of UInt64.MaxValue and UInt64.MinValue


(instead of the standard random.Next which returns an int)


This method MUST be in a position to potentially get any number within that range and not fail to do so due to conversion errors!
LVL 7
Gene_CypAsked:
Who is Participating?
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.

Gene_CypAuthor Commented:
I have created the following but I am unsure it is fully correct:
 

public static UInt64 NextUInt64(this Random rnd, ulong max)
        {
            int rawsize = System.Runtime.InteropServices.Marshal.SizeOf(max);
            var buffer = new byte[rawsize];
            rnd.NextBytes(buffer);
            return BitConverter.ToUInt64(buffer, 0);
        }

Open in new window

0
SuperdaveCommented:
You could do something like
 ( Random.Next * Random.Next ) % ( max - min  ) + min

I don't know C# syntax, but I think you would need to cast Random.Next to UInt64 and by g at % I mean modulo.
 
0
lazyberezovskyCommented:
Usage:
Random64 r = new Random64(new Random(DateTime.Now.Millisecond));
for (int i = 0; i < 20; i++ )
    Console.WriteLine(r.Next(6000000000, 6000000005));
public class Random64
{
    private Random _random;

    public Random64(Random random)
    {
        _random = random;            
    }

    public long Next()
    {
        return Next(Int64.MaxValue);
    }

    public long Next(long maxValue)
    {
        return Next(0, maxValue);
    }

    public long Next(long minValue, long maxValue)
    {
        return (long)(_random.NextDouble() * (maxValue - minValue)) + minValue;
    }
}

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Gene_CypAuthor Commented:
Lazybere, looking to adapt it for ulong (unsigned). Yes, long are very big numbers but hey, ulongs are .. bigger.
Dave, I'll explore your thoughts as well.
0
lazyberezovskyCommented:
Same with ulong:
public class Random64
{
    private Random _random;

    public Random64(Random random)
    {
        _random = random;            
    }

    public ulong Next()
    {
        return Next(UInt64.MaxValue);
    }

    public ulong Next(ulong maxValue)
    {
        return Next(0, maxValue);
    }

    public ulong Next(ulong minValue, ulong maxValue)
    {
        if (maxValue < minValue)
            throw new ArgumentException();
            
        return (ulong)(_random.NextDouble() * (maxValue - minValue)) + minValue;
    }
}

Open in new window

0

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
lazyberezovskyCommented:
And for test:
r.Next(UInt64.MaxValue - 5, UInt64.MaxValue)
0
Gene_CypAuthor Commented:
Yeah, I had already adjusted it, even as I asked. ;)
Thanks for your time guys.
 
0
Gene_CypAuthor Commented:
Very nice and very clear.
0
Gene_CypAuthor Commented:
Lazybere, been looking for ways to increase the "seed pool". Using miliseconds for example limits the seeds between 0-999, meaning only 1000 random paths will be used.
Is there a simple way to have a much larger seed pool?
0
lazyberezovskyCommented:
First idea that comes to my mind is
Guid.NewGuid().GetHashCode()

Or you can use unchecked conversion from DateTime.Now.Ticks to Int32
0
Gene_CypAuthor Commented:
I did the latter but i crop the long to ensure it never gets an overflow exception, so at least the new set of initial seeds is now in the millions.
0
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
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.