CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2004
    Posts
    158

    The best way to get random numbers.

    I want to get the closest I can to choosing true random numbers. Is this simple routine okay, or is there a better way?

    Code:
    Randomize()
    For Me.a = 1 To 250
    listbox1.items.add (Rnd() *a)
    next
    Thanks
    I use VB.Net 2008 in all my wash, for those whiter whites.

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: The best way to get random numbers.

    You should have a look at the Random class in the manual.
    RND is a carry over from VB6 and older, Random is used in the .Net versions
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Apr 2004
    Posts
    158

    Re: The best way to get random numbers.

    Quote Originally Posted by DataMiser View Post
    You should have a look at the Random class in the manual.
    RND is a carry over from VB6 and older, Random is used in the .Net versions
    Really?
    Any chance of a quick code demonstration? It would save me having to go into the loft and dusting of my old books.
    I use VB.Net 2008 in all my wash, for those whiter whites.

  4. #4
    Join Date
    Apr 2004
    Posts
    158

    Re: The best way to get random numbers.

    I am a amateur, but like to now and again make some small programs. It's fun.
    I have been with these forums for quite some years, because I have always found it so friendly and helpful with members always willing to help.
    I use VB.Net 2008 in all my wash, for those whiter whites.

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: The best way to get random numbers.

    The documentation is online, it is very simple to look up the class in your online help and/or search for it on google or where ever.
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: The best way to get random numbers.

    MSDN is very helpful in such cases :

    http://msdn.microsoft.com/en-us/libr...em.random.aspx

    Also, remember that you may still get duplicates with your random numbers. Best would be to keep track of the used numbers and to discard them

  7. #7
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    52

    Re: The best way to get random numbers.

    You can also use RngCryptoServiceProvider to generate random data if needed: http://msdn.microsoft.com/en-us/libr...eprovider.aspx

    The downside I've found with the Random class is it only gives you methods to return random 32 bit integers, and not 64 bit integers. Of course you can also mock up your own wrapper function to return a 64 bit integer, but I've found a useful function that can be P/Invoked to do the same as well. (RtlRandomEx)

    The P/Invoke signature that I've come up with looks like this:
    Code:
    [DllImport("ntdll.dll")]
    internal static extern ulong RtlRandomEx(ref ulong Seed);
    Works quite nice.

    Here are some wrapper methods I quickly put together:
    Code:
    public static ulong RandGen()
    {
    	return RandGen(1);
    }
    
    public static ulong RandGen(ulong seed)
    {
    	return RandGen(seed, 0, ulong.MaxValue);
    }
    
    public static ulong RandGen(ulong seed, ulong lowerBound, ulong upperBound)
    {
    	if (lowerBound >= upperBound)
    	{
    		throw new ArgumentException("The lowerbound argument cannot be equal to or greater than the exclusive upperbound.");
    	}
    	ulong r = NativeMethods.RtlRandomEx(ref seed);
    	return (r % upperBound - lowerBound) + lowerBound;
    }
    Last edited by AceInfinity; October 14th, 2013 at 01:09 AM.
    [sigpic][/sigpic]
    Microsoft MVP .NET Programming (2012 - Present)
    ®Crestron DMC-T Certified Automation Programmer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured