Results 1 to 6 of 6

Thread: Getting a list of random numbers.

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Getting a list of random numbers.

    Hi,

    John Mc offers this as an example of a random 'List(Of Integer)': -
    Code:
    Public Class Form1
    
        Dim rng As New Random
        Dim numbers = Enumerable.Range(1, 75).OrderBy(Function(n) rng.NextDouble()).ToList()
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    'The 'numbers' variable now refers to a List(Of Integer) containing the
     numbers 1 to 75 in random order.
    That's just fine and works well but and placing it between 'Public Class Form1 and 'Private Sub Form1_Load'
    makes it global, accessible from anywhere in the code.
    But how would I go about doing the same thing after the 'To value' has been discovered...
    I tried this, I get no errors but neither do I get a list, I just get the one relevant value of siz(().

    Code:
    Public Class Form1
        Dim place(), myths() As List(Of Integer)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    '	Part of a subroutine, after siz(2) has been established.
    
            For i = 0 To 1
                Dim rng As New Random
                Select Case i
                    Case = 0
                        place = {Enumerable.Range(1, siz(i)).OrderBy(Function(n) rng.NextDouble()).ToList()}
                    Case = 1
                        myths = {Enumerable.Range(1, siz(i)).OrderBy(Function(n) rng.NextDouble()).ToList()}
                End Select
            Next
    Poppa
    Along with the sunshine there has to be a little rain sometime.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Getting a list of random numbers.

    place() is an Array. You’re trying to assign a List

    place = Enumerable.Range(1, siz(i)).OrderBy(Function(n) rng.NextDouble()).ToArray()

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Getting a list of random numbers.

    You do realise that this:
    vb.net Code:
    1. Dim place(), myths() As List(Of Integer)
    is declaring your variables as arrays of Lists, right? If you put the parentheses on the type rather than the variable, as Microsoft now recommends, then you wouldn't make this mistake. You are presumably intending to declare the variables as either 'Integer()' or 'List(Of Integer)', not as 'List(Of Integer)()'. Either declare them as arrays:
    vb.net Code:
    1. Dim place, myths As Integer()
    and then call ToArray or delcare them as Lists:
    vb.net Code:
    1. Dim place, myths As List(Of Integer)
    and call ToList.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Getting a list of random numbers.

    On a different note, it is VERY bad that you have a Random object being created inside a loop. I'm sure that you've been told before to only create one Random object and reuse it rather than creating multiple Random objects and using them once. As your code is, it will likely execute so quickly that both Random objects will produce the exact same sequence of numbers. DO NOT create multiple Random objects where they may be created so quickly that they might use the same seed value. Don't create multiple at all if you can avoid it. The fact that you can't create the arrays at the class level doesn't mean that you can't create the Random object at the class level.

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Getting a list of random numbers.

    Why is there even a loop in the first place?
    Code:
            For i = 0 To 1
                Dim rng As New Random
                Select Case i
                    Case = 0
                        place = {Enumerable.Range(1, siz(i)).OrderBy(Function(n) rng.NextDouble()).ToList()}
                    Case = 1
                        myths = {Enumerable.Range(1, siz(i)).OrderBy(Function(n) rng.NextDouble()).ToList()}
                End Select
            Next
    Should just be:
    Code:
    Dim rng As New Random
    place = {Enumerable.Range(1, siz(i)).OrderBy(Function(n) rng.NextDouble()).ToList()}
    myths = {Enumerable.Range(1, siz(i)).OrderBy(Function(n) rng.NextDouble()).ToList()}

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Getting a list of random numbers.

    Quote Originally Posted by jmcilhinney View Post
    On a different note, it is VERY bad that you have a Random object being created inside a loop. I'm sure that you've been told before to only create one Random object and reuse it rather than creating multiple Random objects and using them once. As your code is, it will likely execute so quickly that both Random objects will produce the exact same sequence of numbers. DO NOT create multiple Random objects where they may be created so quickly that they might use the same seed value. Don't create multiple at all if you can avoid it. The fact that you can't create the arrays at the class level doesn't mean that you can't create the Random object at the class level.
    I agree totally. I can't think of a situation where more than one Random object is ever needed. For my part I follow this practice which ensures that I have only one Random object.

    Code:
        Private Shared PRNG As New Random
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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