DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #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)
    frm.ListBox1.Items.Add(d.ToString)

    Dim b As Integer
    Randomize()
    b = CInt(Int((1000 * Rnd()) + 100))
    frm.ListBox1.Items.Add(b.ToString & vbCrLf)

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

    Sunny,
    Zhiwen_hu@hotmail.com


  2. #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
    frm.ListBox1.Items.Add("")

    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)
    > frm.ListBox1.Items.Add(d.ToString)
    >
    > Dim b As Integer
    > Randomize()
    > b = CInt(Int((1000 * Rnd()) + 100))
    > frm.ListBox1.Items.Add(b.ToString & vbCrLf)
    >
    > frm.ListBox1.Items.Add(vbCrLf)
    > Next
    > frm.ShowDialog()
    > End Sub
    > The code got a amazing results. Could you give me a hand?
    >
    > Sunny,
    > Zhiwen_hu@hotmail.com


  3. #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. #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.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links