1

I'm really confused by a lot of the answers on here pertaining to creating same set of random numbers between function calls.

Some people are saying you will get non-repeating numbers if you seed the random function, other the exact opposite. Can someone please show me how this is done in VB.Net?

Goal:

• I want to have seed: a

• I call the rand function within main: rand(a)

• Given n number of function calls to rand, rand(a) gives set of random numbers A,

• Run ends

• I run main again and I expect to get the same set of random numbers A

Here's some thoughts:

Private Function Generate_Random_Number(Byval Lower as integer, Byval Upper as integer, Byval seed as integer) 
    Dim Random_Value as Integer

    randomize(seed)
    random_value = rand.next(lower, upper + 1)

    Return random value
End Function

Private Sub Main()
    Seed = 100

    For i = 0 to 1 'run this function for two times
        Get_Random_Numbers(Seed)
    Next i
End Sub

Private Sub Get_Random_Numbers(Seed)
    Dim x, y, w, z as integer

    x = Generate_Random_Number(0,1, seed)
    y = Generate_Random_Number(1,2, seed)
    w = Generate_Random_Number(2,3, seed)
    z = Generate_Random_Number(3,4, seed)
End Sub 

Suppose the first function call to Get_Random_Numbers give me {x = 0, y = 1, w = 2, z = 3}, for whatever reason the next call to Get_random numbers give me {x = 1, y = 2, w = 3, z= 4} which is completely different from the first time I called this function!

But I'm using the same seed?

Can someone tell me what I'm doing wrong?

Thank you

David -
  • 2,009
  • 1
  • 13
  • 9
  • You have to use the same seed, then you get the same pseudo random numbers(f.e. always 1000). http://stackoverflow.com/questions/22712456/random-number-generator-generating-same-numbers-each-time-application-is-ran – Tim Schmelter Aug 13 '14 at 21:11
  • @Tim he uses the same seed, but that rand.Next is inexplicable. Where is declared rand? – Steve Aug 13 '14 at 21:12
  • @Steve: he's probably wondering why he gets different numbers everytime he's calling that method. But he gets only the same numbers if he initializes the `Random` with the same seed again not with the same instance. **Edit** Forget this comment, he uses [`Randomize`](http://msdn.microsoft.com/de-de/library/8zedbtdt(v=vs.90).aspx). I haven't used it before. – Tim Schmelter Aug 13 '14 at 21:15
  • To get the behavior you're looking for, use System.Random instead. – Dave Doknjas Aug 13 '14 at 21:21
  • @TimSchmelter my point is that Randomize/Rnd and Random class are not the same (I suppose). I still cannot understand how this code compiles. (At least LinqPAD chokes on that rand.Next line) – Steve Aug 13 '14 at 21:25
  • 1
    @Steve: i think he needs to use [`Rnd()`](http://msdn.microsoft.com/de-de/library/f7s023d2(v=vs.90).aspx) after `Randomize(seed)` instead. But as mentioned, i'm not that familiar with the old VB functions. I guess OP has a mixture and `rand` is a `Ramdom` variable which is reused throughout the application. – Tim Schmelter Aug 13 '14 at 21:28

2 Answers2

2

This code will give you the same sequence of random numbers

Public rand as Random

Private Sub Main()

    Dim Seed = 100
    For i = 0 to 1 
        rand = new Random(seed)
        Get_Random_Numbers()
    Next i
End Sub


Private Sub Get_Random_Numbers()
    Dim x, y, w, z as integer
    x = Generate_Random_Number(0,10)
    y = Generate_Random_Number(10,20)
    w = Generate_Random_Number(20,30)
    z = Generate_Random_Number(30,40)
    Console.WriteLine(x & ", " & y & ", " & w & ", " & z)
End Sub 

Private Function Generate_Random_Number(Byval Lower as integer, Byval Upper as integer) 
    Dim Random_Value as Integer
    Random_Value = rand.Next(lower, upper + 1)
    Return Random_Value
End Function

The main difference is the declaration, at the global level, of a variable of type Random and its initialization with the same seed at the start of every loop. The main problem in your code (forgetting the missing declaration of the variable rand) is the mixing between the Randomize function (of VB6 origin) that requires the Rnd() call and the framework class Random that uses the Next method to originate the next number in the sequence.
Using coherently the Random class and its Next method fix the problem.

If you still prefer to use the Randomize/Rnd functions then you should follow the advice given in the link from other answer. However, keep present, that the Rnd function returns a single with value between 0 and 1. When you assign this single to an integer (thanks to Option Strict being Off) then the Integer could only have a value of 0 or 1. Not so much random.
(The same link provides the way to have the value calculated between the max and min)

Steve
  • 213,761
  • 22
  • 232
  • 286
0

review http://msdn.microsoft.com/en-GB/library/8zedbtdt(v=vs.90).aspx

"To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for Number does not repeat the previous sequence."

user3910810
  • 234
  • 1
  • 6