Showing posts with label VB. Show all posts
Showing posts with label VB. Show all posts

Saturday, September 6, 2008

Use random numbers in Visual Basic .NET

. Saturday, September 6, 2008
0 comments

Microsoft provides excellent support for random numbers, Visual Basic. NET. A key goal is a member of Random most fundamental. NET namespace System. Visual Basic provides the basis of random and Rnd Microsoft.VisualBasic.VBMath tasks. For true randomness, System.Security.Cryptography offers RNGCryptoServiceProvider object.

If this is not enough, it's not that hard to write routines, which provide good random numbers. Tim Patrick's Visual Basic 2005 The message shows how the program is "BetterRandom" category, which is a step in the overall "good enough" pseudo-random numbers provided directly to the random class. NET.

But if the documents for Random class leaves a lot to be desired. Here at their documentation.

Dim bytes1 (99), bytes2 (99) As Byte
Dim rnd1 as new Random ()
Dim rnd2 as new Random ()

rnd1.NextBytes (bytes1)
rnd2.NextBytes (bytes2)

Point of this code is that "because an hour is limited in resolution, using the parameterless constructor create different Random objects in close succession luo random number generators, which produce the same sequence of random numbers. ... For example demonstrates that the two selected objects that are instantiated in close succession to create a similar series of random numbers. "

Different words, since there are no parameters have shifted from Random (), fair value, the system clock is used instead. And because the computer is much faster than the system clock, the same value on the hours when using the two Random () objects are created almost at the same time.

Code to more sense to me (but not the same asia) when I was recoded ga in this manner:

Dim intArray1 (99), intArray2 (99) As Integer
Dim rnd1 as new Random ()
Dim rnd2 as new Random ()

For I Integer = 0 99
intArray1 (i) = rnd1.Next ()
Next
For I Integer = 0 99
intArray2 (i) = rnd2.Next ()
Next

Systems are not in the end, quite the same set of numbers, and this shows one of the problems of "pseudo" random numbers. This declaration oman machine ...

Rnd smoke as a new Random (12345)

... there will always be created in this series ...

143337951
150666398
1663795458
1097663221
1712597933
1776631026
... and so on

Thus, the tables can be divided into fine, but they are not completely "random". This fact, the May be useful sometimes produces the same "accidental" as an example. But, because it is good to avoid the creation of Random object more than once. Everything is easy to create the same sequence of numbers.

VB.NET and provides the basis of random and Rnd function. Partly because it's Visual Basic and provides random and Rnd, and they do the same and asia (but in different syntax), has a lot of confusion in the fact that they are.

Rnd will generate a random number, as well as the functions, no need to declare a root ga ga use.

Dim MyRand As Single = Rnd ()

Random as, if not initialize, VB.NET uses the value of the system clock to the offspring. But if you want to initialize ga, you can use a random. There is no requirement for the use of random, that I do not know. (Despite the statements can be found on the web, on the contrary.) Repeat Rnd calls to use the last number created as a seed value. So, this code works fine for the creation of pseudorandom convenient expression:

Dim sngArray (99) As Single
For I Integer = 0 99
sngArray (i) = Rnd ()
Next

Because it is that and do not object, however, Rnd there is no method or properties. As partial compensation, Microsoft Rnd coded so that different things based on the argument passed. Here's what Rnd luo, if passed, a number that is ...

* Less than zero - the same amount each time, using the code as a seed.
* A higher than zero - the next number of random sequence.
* Zero - most recently produced a number.
* It must not be transferred - to the next number of random sequence.

So, for example, this number will create the same number of times.

Dim MyRand As Single = Rnd ()
Console.WriteLine (MyRand)
Rnd (0)
Console.WriteLine (MyRand)

The bottom line is that here, if you only want something fast, they say, play dice, use Rnd. But, if you need more control, but it has not yet cryptographic quality, use the Random object.

Klik disini untuk melanjutkan »»

Visual Basic Blog

.
0 comments

This isn't only about writi ng interesting code. In the case above, the compiler does work that would ordinarily be done every time the code is executed. If this code were in a loop, the CPU savings could be significant.

In the back of my mind, I had the idea that Join was just simple concatenation, like the & operator. In fact, it only works on arrays of strings. This makes it ideal for creating a CSV file - Comma Separated Values, like those commonly used as input to Excel and other programs. (For those who haven't discovered the joys of XML yet.)

I've previously written about how handy the Split function is in the article, Amazing Splits. But I recently discovered that the companion function, Join can be pretty useful too.


To make it just a bit more challenging, I wrote a program that joins an array of strings into a single string in proper CSV file format and writes it to a sequential file - in just one statement.

Quick ... Don't look ... see if you can write just one statement that does all that.

Wanna look? Here's mine.

My.Computer.FileSystem.WriteAllText( _
My.Computer.FileSystem.CurrentDirectory & _
"\CSVFile.txt", _
"""" & Join(ArrayOfStrings, """,""") & """", _
False)

Klik disini untuk melanjutkan »»
 
Namablogkamu is proudly powered by Blogger.com | Template by o-om.com