Unique random number generator for a mega millions lottery number program assigned as lab

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhsully65
    New Member
    • Nov 2021
    • 1

    Unique random number generator for a mega millions lottery number program assigned as lab

    I was assigned a lab to create a mega millions lottery easy pick number generator. 5 numbers plus 1 mega ball number. I felt i did a really good job however. i received the following comments for my grade of 17 out of 20


    I do not understand what she is saying in this. Not sure if i was supposed to create booleans for each label array? The program works very well without crashing. I feel all the logics are working correctly. Could someone please look and trace. Do i just have a very picky professor? Give me the business if i deserve it...lol

    The array of Booleans should be sized to the values that will be generated => for this project, 70 would be the appropriate upper bound.GenerateR andomNums function should generate only one unique random number. The Return value is not assigned to anything.

    My Code:

    Option Strict On
    Option Explicit On

    Public Class Form1
    Dim lblLabelArray(5 ) As Label 'variable declared to hold values to display in labels
    Dim blnFoundFlagArr ay(71) As Boolean 'class variable declared to flag for found numbers
    Dim objRandom As Random = New Random() 'variable declared to hold the true/false value to validate the unique numbers
    #Region "Click event for Call of Function in Generating Unique Random Numbers"
    Private Sub btnEasyPick_Cli ck(sender As Object, e As EventArgs) Handles btnEasyPick.Cli ck
    'assignment declared to call the function to generate the 5 unique random numbers and then 1 mega number
    GenerateUniqueR andomNum()
    'assigned redim array for button click
    ReDim blnFoundFlagArr ay(71)
    End Sub
    #End Region
    #Region "GenerateUnique RandomNum"

    Function GenerateUniqueR andomNum() As Integer

    'variable declared for mega number
    Dim intMega As Integer
    'variable declared for upper bound of array
    Dim intUpper As Integer = 71
    'vaiable declared for lower bound of array
    Dim intLower As Integer = 1
    ' variable declared for random generator
    Dim intNum As Integer
    'variable declared for counter on loop
    Dim intCount As Integer


    'used for 5 random unique numbers for loop
    For intCount = 0 To 4

    'used to generate all the random numbers
    Do
    'assignment used to generate a randum number
    intNum = objRandom.Next( intLower, intUpper)

    'assignment used to to generate one mega number
    intMega = objRandom.Next( 1, 26)
    ' assignment used to display random number in Label
    lblLabelArray(i ntCount).Text = intNum.ToString
    'assignment used to display the one mega number in label
    lblLabelArray(5 ).Text = intMega.ToStrin g

    'assignment used when boolean finds unique number
    Loop Until blnFoundFlagArr ay(intNum) = False
    'assignment used to flag boolean true to keep unique number
    blnFoundFlagArr ay(intNum) = True


    Next

    Return intNum
    End Function

    #End Region

    #Region " Form load for labels"
    Private Sub Form1_Load(send er As Object, e As EventArgs) Handles Me.Load
    'these are used to assign array calues to the labels at form load
    lblLabelArray(0 ) = lblLabel0
    lblLabelArray(1 ) = lblLabel1
    lblLabelArray(2 ) = lblLabel2
    lblLabelArray(3 ) = lblLabel3
    lblLabelArray(4 ) = lblLabel4
    lblLabelArray(5 ) = lblLabel5
    End Sub
    #End Region
    End Class
  • Smitty099099
    New Member
    • Mar 2022
    • 5

    #2
    Private Sub Button1_Click(s ender As Object, e As EventArgs) Handles Button1.Click
    Randomize()
    Dim num1 As Integer
    Dim num2 As Integer
    Dim num3 As Integer
    Dim num4 As Integer
    Dim num5 As Integer
    num1 = Int(Rnd() * 70 + 1)
    num2 = Int(Rnd() * 70 + 1)
    num3 = Int(Rnd() * 70 + 1)
    num4 = Int(Rnd() * 70 + 1)
    num5 = Int(Rnd() * 70 + 1)
    TextBox1.Text = CStr(num1)
    TextBox2.Text = CStr(num2)
    TextBox3.Text = CStr(num3)
    TextBox4.Text = CStr(num4)
    TextBox5.Text = CStr(num5)
    End Sub

    Comment

    • Smitty099099
      New Member
      • Mar 2022
      • 5

      #3
      Actually Int(Rnd() * 70 + 1) produces numbers 1 thru 70

      Int(Rnd() * 71) produces numbers 0 thru 70

      the first one is 70 numbers starting at 1

      the second one is 71 numbers starting at 0
      0 is counted as a number so you have 71 possibilities

      I could have edited the first post but thought this added post would help explain how the rnd function works

      also instead of cstr(num1) it could be num1.ToString()
      Last edited by Smitty099099; Mar 3 '22, 08:07 AM. Reason: addition and explaination

      Comment

      Working...