# Thread: Getting a list of random numbers.

1. ## 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()

'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)

'	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

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()

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.

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.

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

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