0

I have two functions: The first one generates random numbers and the second one make a simulation to approximate the value of pi.

Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Double

        Static Generator As System.Random = New System.Random()
        Return Generator.Next(Min, Max) / (Max - Min)



    End Function

Then, the first function is inside the second function to generate the random values. What I want is sampling without repetition:

 Public Function aproxpi(n As Integer) As Double
        Dim contador As Integer = 0




        Dim vector(n, 2) As Double



        For i = 0 To n
  ' (0, 700) is a tuning parameter, I've seen that if I choose ( 0,10000) there's a less precise approximation due to repatead values
            vector(i, 1) = GetRandom(0, 700)
            vector(i, 2) = GetRandom(0, 700)
            If (vector(i, 1) ^ 2 + vector(i, 2) ^ 2) < 1 Then
                contador = contador + 1


            End If
        Next
        aproxpi = 4 * (contador / n)


    End Function

vector(i,1) and vector(i,2) are an (x,y) pair. So I don't want (x,y) pairs repeated.

So, how can I Avod repeated values in my code?

CreamStat
  • 2,155
  • 6
  • 27
  • 43
  • 2
    Did you try a google search on "vb.net generate random number without repetition"? For example, http://stackoverflow.com/questions/17666175/random-numbers-in-array-without-any-duplicates, also what's `contador` ? – Victor Zakharov Dec 05 '14 at 16:27
  • Seems like you're looking for a shuffle algorithm, not a random algorithm. If your "min" is always 0, why not just get a random number from 0 to 1? – the_lotus Dec 05 '14 at 16:30
  • Contador counts the number of points inside a quarter circle and n is the number of points inside a square. – CreamStat Dec 05 '14 at 16:31
  • I don't know that term. But if that term would help me sampling (x,y) pairs without replacement with the restriction 0 – CreamStat Dec 05 '14 at 16:38
  • You don't necessarily want to avoid repeated values, it is possible for two truly random values to be the same. You want to avoid to avoid repeated values because `Random` is seeded from the clock. This is a known frailty. http://stackoverflow.com/a/1654902/659190 – Jodrell Dec 05 '14 at 16:57

1 Answers1

0

If Min is always 0 then you can just get a random number from 0 to 1. If you get a random double from 0 to 1 instead of an integer, you'll have a much higher chance of not getting the same number twice. On top of that, I don't know why you store all the previous numbers, it's not needed in your example.

Here's why I see you can do to change it.

ApproximatePI(100000)

Public Function ApproximatePI(ByVal totalIteration As Integer) As Double

    Dim r As New Random()
    Dim insideCircle As Integer

    insideCircle = 0

    For n As Integer = 0 To totalIteration
        Dim x As Double = r.NextDouble()
        Dim y As Double = r.NextDouble()

        If (x ^ 2 + y ^ 2) < 1 Then
            insideCircle += 1
        End If
    Next

    Return (4.0 * insideCircle) / totalIteration
End Function
the_lotus
  • 12,668
  • 3
  • 36
  • 53