0

How to insert another instruction to be able to avoid repetition of number as all the six numbers should be different from each other?

Here is the code of my Visual Basic module:

Module Loto

    Sub Main()
        Dim Value1, Value2, Value3, Value4, Value5, Value6 As Integer

        Console.WriteLine("Example 3: To generate six numbers from 1 to 40")
        Console.WriteLine()

        'Initialize the random-number generator.
        Randomize()

        'Generate a random value between 1 and 40.
        'Int((HighestValue - LowestValue + 1) * Rnd) + LowestValue

        Value1 = CInt(Int((40 * Rnd()) + 1))
        Value2 = CInt(Int((40 * Rnd()) + 1))
        Value3 = CInt(Int((40 * Rnd()) + 1))
        Value4 = CInt(Int((40 * Rnd()) + 1))
        Value5 = CInt(Int((40 * Rnd()) + 1))
        Value6 = CInt(Int((40 * Rnd()) + 1))


        Do While (Value1 = Value2)
            Value2 = CInt(Int((40 * Rnd()) + 1))
        Loop

        Console.WriteLine("Random number generated is " & Value1)
        Console.WriteLine("Random number generated is " & Value2)
        Console.WriteLine("Random number generated is " & Value3)
        Console.WriteLine("Random number generated is " & Value4)
        Console.WriteLine("Random number generated is " & Value5)
        Console.WriteLine("Random number generated is " & Value6)

        Console.ReadLine()
    End Sub
End Module
Mofi
  • 46,139
  • 17
  • 80
  • 143

5 Answers5

1

.NET provides for a better random generator than the legacy VB one. Basically, you want to create an array of values and swap them so that no number repeats.

This is called a shuffle since you are not actually picking random numbers but putting a set of values (like a deck of cards or a series of numbers) into a random order.

Dim rValues(39) As Integer               ' random values holder
Dim rand = New Random()                  ' NET random generator - do this ONCE
Dim temp As integer

Shuffle an array of integers:

For ndx As Int32 = rValues.Length - 1 To 0 Step-1
  Dim r = rand.Next(n + 1)               ' next value

  temp = rValues(ndx)                    ' swap values
  rValues(ndx) = rValues(r)
  rValues(r) = temp
Next

If used for something like a deck of cards, pick values starting at rValues(0) until you get to the end. To get just 6, take the first ones there wont be any repeats.

As an in-place shuffle, it is very efficient.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
1

I like the direction Plutonix mentioned, i.e. using Random class, yet it's possible to make it even shorter:

Dim r As New Random
Dim a = Enumerable.Range(1, 40)
Dim b = a.OrderBy(Function() r.Next).Take(6)
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
1

My $.02

Private Shared prng As New Random

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim a As List(Of Integer) = (From x In Enumerable.Range(1, 40)
                                    Order By prng.Next Take 6).ToList


    For Each n As Integer In a
        Debug.WriteLine(n)
    Next
End Sub
dbasnett
  • 11,334
  • 2
  • 25
  • 33
0

create an array 1..40, get a random number 1..40, exchange the value of that position with the value at pos(40), get a random number 1..39 and exchange that value with pos(39) ... in the end you have the numbers at pos 35..40

  • It would be helpful to have this in code, or in pseudo-code in the answer itself, but other than that this is indeed how to create a random permutation of the numbers 1 to 40. – Sumurai8 May 31 '14 at 12:36
0

create an array for your answers and check it after every round:

dim resultArr(6) as integer
dim unique as boolean
for i=0 to 5 
  unique=false
  while not unique
    resultArr(i)=CInt(Int((40 * Rnd()) + 1))
    for j=0 to i-1
      unique=(unique OR resultArr(j)<>resultArr(i))
    next
  wend
next