-
Oct 5th, 2022, 05:43 PM
#1
Thread Starter
PowerPoster
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.
-
Oct 5th, 2022, 07:57 PM
#2
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()
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 5th, 2022, 09:12 PM
#3
Re: Getting a list of random numbers.
You do realise that this:
vb.net Code:
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:
Dim place, myths As Integer()
and then call ToArray or delcare them as Lists:
vb.net Code:
Dim place, myths As List(Of Integer)
and call ToList.
-
Oct 5th, 2022, 09:16 PM
#4
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.
-
Oct 6th, 2022, 08:05 AM
#5
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
-
Oct 6th, 2022, 08:12 AM
#6
Re: Getting a list of random numbers.
Originally Posted by jmcilhinney
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
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
|