Results 1 to 6 of 6

Thread: More help needed to add options to RANDOM NUMBER GENERATOR app

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    141

    More help needed to add options to RANDOM NUMBER GENERATOR app

    Hey all,

    I am wanting to add some more coding options to my random number generator app that basically has 2 screens, a control screen to input a low / high number with a generate button.
    And an output screen which runs on the secondary monitor which shows the generated number.

    The app works fine, but has it's limitations. The people who use the app have given some good feedback with some things they need added, so hopefully I can get some help.

    vb Code:
    1. Friend Module Module1
    2.     Friend F1 As Form1
    3.     Friend F2 As Form2
    4. End Module
    5.  
    6. Public Class Form1
    7.     Private Declare Sub keybd_event Lib "user32" _
    8.                                (ByVal bVk As Byte, _
    9.                                 ByVal bScan As Byte, _
    10.                                 ByVal dwFlags As Byte, _
    11.                                 ByVal dwExtraInfo As Byte)
    12.     Private Const VK_CONTROL As Byte = &H11
    13.     Private Const VK_8 As Byte = &H38
    14.     Private Const KEYEVENTF_KEYDOWN As Byte = &H0
    15.     Private Const KEYEVENTF_KEYUP As Byte = &H2
    16.     Dim list As New ArrayList
    17.  
    18.  
    19.  
    20.     Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    21.         'low Number
    22.     End Sub
    23.  
    24.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    25.         'High Number
    26.     End Sub
    27.  
    28.     Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
    29.         'Generated Number
    30.  
    31.         If F2 Is Nothing Then
    32.             F2 = New Form2()
    33.             F2.Show()
    34.  
    35.         Else
    36.             F2.TextBox1.Text = TextBox3.Text
    37.         End If
    38.     End Sub
    39.  
    40.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    41.         'Listbox
    42.     End Sub
    43.  
    44.  
    45.  
    46.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    47.  
    48.         Dim Low As Integer = 1
    49.         Dim High As Integer
    50.         High = TextBox1.Text
    51.         Low = TextBox2.Text
    52.  
    53.         For i As Integer = Low To High
    54.             'Add the numbers to the collection.
    55.             list.Add(i)
    56.         Next i
    57.         Dim rand As New Random
    58.         Dim index As Integer
    59.         Dim item As Object
    60.  
    61.         'Display the items in random order.
    62.         'While list.Count > 0
    63.         'Choose a random index.
    64.         index = rand.Next(0, list.Count)
    65.  
    66.         'Get the item at that index.
    67.         item = list(index)
    68.  
    69.         'Remove the item so that it cannot be chosen again.
    70.         list.RemoveAt(index)
    71.  
    72.         'Display the item.
    73.         TextBox3.Text = item.ToString
    74.         ListBox1.Items.Add(TextBox3.Text)
    75.         'End While
    76.         Form4.Close()
    77.         Form4.Show()
    78.         tmrWait.Interval = 5000
    79.         tmrWait.Start()
    80.  
    81.  
    82.     End Sub
    83.  
    84.  
    85.  
    86.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    87.  
    88.  
    89.  
    90.         Dim tmrWait As New Timer
    91.  
    92.         F1 = Me
    93.  
    94.         If F2 Is Nothing Then
    95.             F2 = New Form2()
    96.             F2.Show()
    97.  
    98.         End If
    99.     End Sub
    100.  
    101.     Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
    102.         'Enter your own title
    103.  
    104.         If F2 Is Nothing Then
    105.             F2 = New Form2()
    106.             F2.Show()
    107.  
    108.         Else
    109.             F2.TextBox2.Text = TextBox4.Text
    110.         End If
    111.     End Sub
    112.  
    113.  
    114.  
    115.     Private Sub tmrWait_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrWait.Tick
    116.         Form4.Close()
    117.         tmrWait.Stop()
    118.     End Sub
    119.  
    120.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    121.         Me.Close()
    122.     End Sub
    123.  
    124.  
    125. End Class

    The needed additions are:

    • Ability to exclude certain numbers from being generated. So they input a low / high number, but then also maybe another textbox with an add button that adds the number to the List.Count
    • The ability to click a tickbox / checkbox (or similar) and have the app be able to generate the same number twice. As it stands it will generate a list and then work through it. Some people want the ability to run a random number draw where you have more than 1 chance to win


    I know I should be posting code and then getting feedback on how to make it work, but I am on a plane tomorrow and am under pressure to get this done.
    Some feedback on if it is possible, and maybe an example or too that I can try implement would be fantastic?!

    Appreciate any replies

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    141

    Re: More help needed to add options to RANDOM NUMBER GENERATOR app

    Here is my Form1 app in Design
    Name:  randomnumber.jpg
Views: 114
Size:  47.0 KB

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    141

    Re: More help needed to add options to RANDOM NUMBER GENERATOR app

    Anyone have any further ideas? I will be back home in the office in the next few days and will be getting into this early next week..
    Feedback would be ideal..

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: More help needed to add options to RANDOM NUMBER GENERATOR app

    First, replace the TextBoxes for the low/high input to NumericUpDowns. This will prevent a user from entering in an invalid Integer. Also, don't declare your Random object inside the button's click. Instead declare it at the Form level, this will prevent seeding issues.

    Ability to exclude certain numbers from being generated. So they input a low / high number, but then also maybe another textbox with an add button that adds the number to the List.Count
    Add a NumericUpDown and a ListBox. The NumericUpDown will allow the user to enter in a value where as the ListBox will display the excluded numbers. Whenever the user clicks on the Generate button, use this:
    Code:
    Dim exclusions As List(Of Integer) = New List(Of Integer)
    For Each itm As String in ExcludedListBox.Items
        exclusions.Add(itm)
    Next
    
    Dim randomInt As Integer
    
    Do
        randomInt = r.Next(lowInput, highInput)
    Loop Until Not exclusions.Contains(randomInt)
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    141

    Re: More help needed to add options to RANDOM NUMBER GENERATOR app

    Hey thanks for the reply, sorry only just getting to it.

    All seems to work but I cant seem to declare 'r'?
    Code:
    randomInt = r.Next(lowInput, highInput)
    I tried:

    Code:
    Dim r As New Random
    But was no help, what am I missing...

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: More help needed to add options to RANDOM NUMBER GENERATOR app

    Where did you put that line? With Dim, it would suggest that it was a local variable (inside a method). You want it to be a form level variable. It probably won't matter in this case, since people may not hit the button fast enough to cause trouble with a local variable, but trouble could occur. The issue is that whenever a Random object is created, it is seeded with the current system time down to the second. If a random number generator is seeded with the same seed, it will return the same sequence of numbers. So, if you create two Random objects within one second, they will both get the same system time as a seed, and will produce identical strings of numbers. Having only one Random object at form level is the way to avoid this. However, while DDay used the variable name r for the Random object, I much prefer the more descriptive name that you used in your original post, so I'd go with the name you had, just move it to form scope.

    At some point, you're going to want to turn Option Strict ON at the project level (Project|Properties on the Compile tab). This will provide better performance, though the difference will be too small to notice in this app, since the app will spend most of its time waiting for the user to do something. Still, it will be slightly better. You will also get several errors due to your use of implicit conversions. Implicit conversions, such as assigning a string from a textbox to an Integer variable, are slower than explicit conversions, even though they require slightly less typing on your part. However, they are also error prone. In your case, as DDay noted, the user could enter something that isn't an integer, at which point the app will simply crash due to an invalid conversion. Using a NumericUpDown control will help with this, since the control will only accept numbers, but you'd still be using an implicit cast from Decimal to Integer...which will always work, just not quite as quick as an explicit cast.

    As to your initial points, one of them is pretty easy. To allow for multiple hits on the same number, just use the value from Random.Next, rather than using a list. The list is there to ensure that each item is used once and only once. If you don't want them to be once and only once, then the simplest answer is just to use the straight random number in the range you have specified. The problem that the users may see is that a truly random number is just that. Therefore, if you have a range from 1 to 10, there is a chance of getting 1, 1, 1, 1, 1, 1,....1...etc. The chance is small (1/10 * 1/10 * 1/10....etc), but not zero. Randomly choosing elements from a set means no repeats. True random numbers would mean any possible pattern at all. Getting something intermediate between those two is both tricky, and highly dependent on exactly what pattern you are trying to achieve.

    Excluding certain numbers might best be done by showing the user the list of numbers that could be generated (low to high, in this case), then allowing them to select numbers that should be excluded. This could be done with a multi-select listbox, and you already appear to have a listbox, though it may be showing the numbers chosen rather than the numbers in the possible set. Removing the exclusion numbers from the set is easy enough, but if the user wants a true random number AND the ability to exclude some numbers in the range, then what you'd have to do is get a random number, check whether it is in the exclusion set, and if it is, get a new random number. If the exclusion set is large, this could become inefficient, but it will probably be pretty doggone fast even if you had a range from 1 to 100 and excluded all but 98 and 99.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width