To Richard Grier

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Sunny Guest

## To Richard Grier

Hi,
I want to use the System.Random class to get a random number, but I got a
amazing thing. The code as follow:
Sub Main()
Dim frm As New Form1()
Dim i As Integer
For i = 0 To 5
Dim rand As New Random(CInt(Date.Now.Ticks And _ Integer.MaxValue))
Dim d As Integer
d = rand.Next(100, 1000)

Dim b As Integer
Randomize()
b = CInt(Int((1000 * Rnd()) + 100))

Next
frm.ShowDialog()
End Sub
The code got a amazing results. Could you give me a hand?

Sunny,
Zhiwen_hu@hotmail.com

2. Jason Sobell Guest

## Re: To Richard Grier

My name's not Richard but a quick look shows one main problem:

Move the 'Dim rand' statement to the start of your code (outside the loop)
so that it is only called once. The reason you get repeating numbers is
that your 5 iterations usually all occur within a single 'tick' so you are
seeding the random number with the same value each time.

You are adding items to a listbox, so you don't need VbCrLf (that's what
those two odd box shapes are). If you want to add a blank line, just use

Cheers,
Jason

On 13 May 2003 04:03:33 -0700, Sunny wrote:
>
> Hi,
> I want to use the System.Random class to get a random number, but I got a
> amazing thing. The code as follow:
> Sub Main()
> Dim frm As New Form1()
> Dim i As Integer
> For i = 0 To 5
> Dim rand As New Random(CInt(Date.Now.Ticks And _ Integer.MaxValue))
> Dim d As Integer
> d = rand.Next(100, 1000)
>
> Dim b As Integer
> Randomize()
> b = CInt(Int((1000 * Rnd()) + 100))
>
> Next
> frm.ShowDialog()
> End Sub
> The code got a amazing results. Could you give me a hand?
>
> Sunny,
> Zhiwen_hu@hotmail.com

3. Phil Weber Guest

## Re: To Richard Grier

Sunny: It's best to include a description of your question in the subject of
your posts, rather than posting them to a single individual. For more
suggestions on how to get the most out of these discussion groups, see
http://news.devx.com/newspolicy.asp . Thanks!
--
Phil Weber

4. Dick Grier Guest

## Re: To Richard Grier

Hi,

I see that Jason and Phil have posted a responses. Both seem to be on
target.

****

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise