-
October 6th, 2013, 12:42 AM
#1
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.
-
October 6th, 2013, 01:34 AM
#2
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.
-
October 6th, 2013, 01:37 AM
#3
Re: The best way to get random numbers.
Originally Posted by DataMiser
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.
-
October 6th, 2013, 03:58 AM
#4
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.
-
October 6th, 2013, 09:08 AM
#5
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.
-
October 7th, 2013, 07:37 AM
#6
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
-
October 13th, 2013, 11:34 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|