-
Aug 3rd, 2014, 05:42 PM
#1
Thread Starter
Addicted Member
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:
Friend Module Module1
Friend F1 As Form1
Friend F2 As Form2
End Module
Public Class Form1
Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Byte, _
ByVal dwExtraInfo As Byte)
Private Const VK_CONTROL As Byte = &H11
Private Const VK_8 As Byte = &H38
Private Const KEYEVENTF_KEYDOWN As Byte = &H0
Private Const KEYEVENTF_KEYUP As Byte = &H2
Dim list As New ArrayList
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
'low Number
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
'High Number
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
'Generated Number
If F2 Is Nothing Then
F2 = New Form2()
F2.Show()
Else
F2.TextBox1.Text = TextBox3.Text
End If
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
'Listbox
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Low As Integer = 1
Dim High As Integer
High = TextBox1.Text
Low = TextBox2.Text
For i As Integer = Low To High
'Add the numbers to the collection.
list.Add(i)
Next i
Dim rand As New Random
Dim index As Integer
Dim item As Object
'Display the items in random order.
'While list.Count > 0
'Choose a random index.
index = rand.Next(0, list.Count)
'Get the item at that index.
item = list(index)
'Remove the item so that it cannot be chosen again.
list.RemoveAt(index)
'Display the item.
TextBox3.Text = item.ToString
ListBox1.Items.Add(TextBox3.Text)
'End While
Form4.Close()
Form4.Show()
tmrWait.Interval = 5000
tmrWait.Start()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tmrWait As New Timer
F1 = Me
If F2 Is Nothing Then
F2 = New Form2()
F2.Show()
End If
End Sub
Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
'Enter your own title
If F2 Is Nothing Then
F2 = New Form2()
F2.Show()
Else
F2.TextBox2.Text = TextBox4.Text
End If
End Sub
Private Sub tmrWait_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrWait.Tick
Form4.Close()
tmrWait.Stop()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
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
-
Aug 3rd, 2014, 05:44 PM
#2
Thread Starter
Addicted Member
Re: More help needed to add options to RANDOM NUMBER GENERATOR app
Here is my Form1 app in Design
-
Aug 26th, 2014, 11:16 AM
#3
Thread Starter
Addicted Member
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..
-
Aug 26th, 2014, 11:24 AM
#4
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)
-
Sep 1st, 2014, 05:02 PM
#5
Thread Starter
Addicted Member
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...
-
Sep 1st, 2014, 05:41 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|