|
-
Nov 23rd, 2010, 09:23 PM
#1
Thread Starter
Addicted Member
Random Number Generator - Store generated numbers
Right so my generator is working, but I now want to take the numbers that it generates and add them to a list of generated numbers. So you can monitor what numbers have been generated.
Then ideally a way to exclude these numbers from being generated again.
Heres my code for my generate button
vb Code:
Dim Low As Integer = 1
Dim High As Integer
High = HighNum.Text
Low = TextBox2.Text
Get_Random(Low, High)
TextBox1.Visible = True
Beep()
And my Random Number code
vb Code:
Sub Get_Random(ByVal Low As Integer, ByVal High As Integer)
TextBox1.Text = Math.Floor((High - Low + 1) * Rnd() + Low)
End Sub
Can I then somehow work in a, if number is in listbox regenerate?
-
Nov 23rd, 2010, 09:58 PM
#2
Re: Random Number Generator - Store generated numbers
You have basically three options:
1. Generate the whole list first, generate random numbers and remove those from the list as you go.
2. Generate the whole list first, order it randomly and then use the items sequentially.
3. Generate random numbers and add the corresponding items to a list. Each time you generate a number, check whether it's in the list of those already used and discard it if it is.
Option 1 is the best choice if you need to make a large number of random selections without using all items. Option 2 is best if you will use every item. Option 3 is OK if you will only use a small number of items.
For examples of options 1 and 2, follow the CodeBank link in my signature and search for the word "random" in the thread titles. You'll find two that apply.
-
Nov 24th, 2010, 03:47 PM
#3
Thread Starter
Addicted Member
Re: Random Number Generator - Store generated numbers
Alright so I am going to attempt to use your code "Unique, Random Selections from a List" and try and change it a bit to suit my needs. So far I added my code just to the start to allow user input values.
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim list As New ArrayList
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.
MessageBox.Show(item.ToString())
End While
End Sub
From here once I click the generate button it generates random numbers into a message box, but will generate all x amount depending on the input values. I need to be able to change this to only generate once, once the button is pressed. And also for the results to be shown in either a text box or label so that I can work in my multiforms.
vb Code:
MessageBox.Show(item.ToString())
Now I have tried changing this value to the following but I cannot get it to work without errors. The errors being with (item.ToString()).
Am I just not using this the proper way?
vb Code:
TextBox1.Text(item.ToString())
TextBox1.Show(item.ToString())
And for the removing from the list, is it possible to change this to add it to a listbox of generated numbers aswell as removing it from the list so it wont be regenerated?
-
Nov 24th, 2010, 05:12 PM
#4
Thread Starter
Addicted Member
Re: Random Number Generator - Store generated numbers
I have now changed the output to suit my multiform layout. This is what I used to get it to work.
vb Code:
TextBox3.Text = item.ToString
The only real thing left to do is to incorporate the listbox to show these generated numbers. Also I have noticed that it still will generate numbers that have been done before. If I set the low and high to say 10 - 20 it will generate the same numbers twice a few times when ther are still un generated numbers sitting there.
Maybe this is to do with how I am letting the user input these numbers?
Last edited by Noob script kiddie; Nov 24th, 2010 at 05:23 PM.
-
Nov 24th, 2010, 05:48 PM
#5
Thread Starter
Addicted Member
Re: Random Number Generator - Store generated numbers
I hope you come back soon 
I have added the following code to the generate button and it sends the generated numbers to the listbox.
vb Code:
ListBox1.Items.Add(TextBox3.Text)
Last edited by Noob script kiddie; Nov 24th, 2010 at 06:01 PM.
Reason: Changed some things since posting
-
Dec 6th, 2010, 09:06 PM
#6
Re: Random Number Generator - Store generated numbers
Some implementation notes:
 Originally Posted by jmcilhinney
1. Generate the whole list first, generate random numbers and remove those from the list as you go.
Since you will be doing index-based access on the collection, use an array rather than a List. To perform removing when dealing with an array, simply consider a subset of the array each time. That is, keep a track of how many items you have left to return. On each iteration, generate a random integer from 0 (inclusive) to number of items left to return (exclusive). Use that as the index of the element to return this iteration, then overwrite its value with the value of the last element you are currently considering. Next time through, you consider the range of the array without that last element.
It would end up looking something like this as an example run: (Bold elements are the range of the array we are interested in each iteration, Red element is the element that was overwritten at the end of the previous iteration)
Code:
Array: | # | |
0 1 2 3 4 5 6 7 8 9 | Rem | Rnd | Returned
---------------------+-----+-----+----------
0 1 2 3 4 5 6 7 8 9 | 10 | 3 | 3
0 1 2 9 4 5 6 7 8 9 | 9 | 0 | 0
8 1 2 9 4 5 6 7 8 9 | 8 | 7 | 7
8 1 2 9 4 5 6 7 8 9 | 7 | 1 | 1
8 6 2 9 4 5 6 7 8 9 | 6 | 4 | 4
8 6 2 9 5 5 6 7 8 9 | 5 | 1 | 6
8 5 2 9 5 5 6 7 8 9 | 4 | 0 | 8
9 5 2 9 5 5 6 7 8 9 | 3 | 1 | 5
9 2 2 9 5 5 6 7 8 9 | 2 | 1 | 2
9 2 2 9 5 5 6 7 8 9 | 1 | 0 | 9
 Originally Posted by jmcilhinney
2. Generate the whole list first, order it randomly and then use the items sequentially.
This is not actually any better in terms of performance than the first option in the case where you select the whole list, if you code the first option correctly. You need to be careful that you do not introduce a sampling bias (i.e. some orders are more likely to occur than other orders). This can be the most easily implemented, however.
 Originally Posted by jmcilhinney
3. Generate random numbers and add the corresponding items to a list. Each time you generate a number, check whether it's in the list of those already used and discard it if it is.
Use a HashSet for this, not a List. The check as to whether it is in the list will be constant time, rather than increasing linearly with List size.
Last edited by Evil_Giraffe; Dec 6th, 2010 at 09:10 PM.
-
Nov 25th, 2010, 02:36 AM
#7
Re: Random Number Generator - Store generated numbers
By using the code to populate the "list" inside the Sub Button1_Click you regenerate this list each time you click that button. You need to declare the list in the general area in order to keep it after the Button1_Click has been run and to be able to use it on the next click.
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Nov 28th, 2010, 08:52 PM
#8
Thread Starter
Addicted Member
Re: Random Number Generator - Store generated numbers
Alright so I thought I had it working, but turns out I don't.
I read what Opus wrote, and from what I can understand I would need to move the creating of the list to somewhere other than the button. I tried this by moving the following to the form1 load sub:
vb Code:
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
This causes more problems, so where exactly should I move it/should I even be moving it at all.
Last edited by Noob script kiddie; Nov 28th, 2010 at 09:24 PM.
-
Nov 29th, 2010, 12:37 AM
#9
Re: Random Number Generator - Store generated numbers
What kind of "more Problems" do you get?
Without seeing all your code, we can't tell.
BTW: You are mioxing up Strings and number-type variables(High get the text[i.e. String] from a textbox, but it is used as if it is an Integer [for i as integer = Low to High])
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Nov 29th, 2010, 03:46 PM
#10
Thread Starter
Addicted Member
Re: Random Number Generator - Store generated numbers
Here is my entire code for form1. At the moment, it generates a new list every time I click generate.
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
Form3.Close()
Form3.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
Form3.Close()
tmrWait.Stop()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYDOWN, 0)
keybd_event(VK_8, 0, KEYEVENTF_KEYDOWN, 0)
keybd_event(VK_8, 0, KEYEVENTF_KEYUP, 0)
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)
Me.Close()
End Sub
Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class
If you look at my button_1 you can see that I have disabled the lines
vb Code:
While list.Count > 0d
End While
If I allow these, the generated list will generate all numbers (depending on values) to the listbox. So say I enter 10-20 in the low and high boxes, it would display all 11 possible values in the listbox box.
Disabling it like I have done, it seems to generate 1 number to the listbox. However then you get double ups. So maybe this could be where its going wrong?
-
Nov 29th, 2010, 04:30 PM
#11
Re: Random Number Generator - Store generated numbers
You'll want to have a way to generate a new list of numbers outside of your button click. In the small example here I chose the .Leave event for the TextBoxes to create a new list. Whatever you do you'll want more error checking (is highValue > lowValue etc.)
Code:
Public Class Form1
Dim numbers As List(Of Integer)
Private Sub TextBox_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles lowValueTextBox.Leave, highValueTextBox.Leave
If Integer.TryParse(lowValueTextBox.Text, Nothing) AndAlso Integer.TryParse(highValueTextBox.Text, Nothing) Then
numbers = Enumerable.Range(CInt(lowValueTextBox.Text), CInt(highValueTextBox.Text) - CInt(lowValueTextBox.Text) + 1).ToList
GeneratedNumbersListBox.Items.Clear()
End If
End Sub
Private Sub GenerateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles GenerateButton.Click
If numbers.Count > 0 Then
Dim rnd As New Random
Dim idx As Integer = rnd.Next(0, numbers.Count)
GeneratedNumbersListBox.Items.Add(numbers.Item(idx))
numbers.RemoveAt(idx)
Else
MessageBox.Show("No more numbers to choose from the range")
End If
End Sub
End Class
-
Nov 29th, 2010, 05:24 PM
#12
Thread Starter
Addicted Member
Re: Random Number Generator - Store generated numbers
Alright, I'll give this a go now and see what I can get to work..
Cheers
Last edited by Noob script kiddie; Nov 29th, 2010 at 05:35 PM.
-
Nov 29th, 2010, 05:53 PM
#13
Thread Starter
Addicted Member
Re: Random Number Generator - Store generated numbers
That seems to be working fine. Couple of things though, I need to get the latest generated number to appear in a box that will display the number on the control screen. I used to get it to work by using,
vb Code:
TextBox3.Text = item.ToString
However this causes the error Object reference not set to an instance of an object.
Have you got any suggestions?
-
Nov 29th, 2010, 06:38 PM
#14
Re: Random Number Generator - Store generated numbers
I'll need a little more context than that 1 line to answer that. Mind posting the rest of your code so I can take a look?
-
Nov 29th, 2010, 06:53 PM
#15
Thread Starter
Addicted Member
Re: Random Number Generator - Store generated numbers
Well this used to be my generate button:
vb Code:
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
Form3.Close()
Form3.Show()
tmrWait.Interval = 5000
tmrWait.Start()
End Sub
And more specifically these two lines:
vb Code:
TextBox3.Text = item.ToString
ListBox1.Items.Add(TextBox3.Text)
This would then put the generated number in the listbox and textbox. The reason I need it in both is that I duplicate the text from the textbox and copy it to another form that is my display screen.
That help a bit more?
-
Nov 30th, 2010, 12:31 AM
#16
Re: Random Number Generator - Store generated numbers
As long as you are only copying new lines of code into your old (copied?) code, the problems will persist!
As MattP (Post#10) and I(Post#6) have stated you need to run the code to generate the list only once! since you are doing that each time you click your Button1 it won't change!
Remember your list holding all the numbers is alled "list", why are you using a new one called "ListBox1" now?
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Nov 30th, 2010, 06:37 AM
#17
Frenzied Member
Re: Random Number Generator - Store generated numbers
you are getting all messed up
why don't you atsrt again and apply what you are being told in you own way rather that altering code you don't understand?
my penneth...
make a setup button and a get random button
enter you mins and maxes
create an array to hold the random values
pressing setup will fill the array and then using a pair of random numbers equal to the size of the array
use the option no 3 given to you by the first respondant to swap the contents of the array around
this gives you your randomness
now simple read off the array when the get random is pressed keeping a count of where you are in the array.
reporting the value from the array in the output text box as required
all seems simple to us but we have been doing this for 35 years or so!
-
Nov 30th, 2010, 07:58 AM
#18
Re: Random Number Generator - Store generated numbers
Code:
Public Class Form1
Dim foo As CommonRandomQ
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'creation
Dim lo, hi As Integer
Dim err As Boolean = False
'validate user input
If Not Integer.TryParse(TextBox1.Text, lo) Then
err = True 'not a number
End If
If Not Integer.TryParse(TextBox2.Text, hi) Then
err = True 'not a number
End If
If Not err Then
foo = New CommonRandomQ(Math.Min(lo, hi), Math.Max(lo, hi))
ListBox2.Items.Clear()
lb1Upd()
Label1.Text = String.Format("Generated {0} numbers.", foo.CountRemaining.ToString("n0"))
Else
Label1.Text = "Only numbers"
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
'pick one
If foo.CountRemaining > 0 Then ListBox2.Items.Add(foo.NextRandom)
lb1Upd()
Label1.Text = String.Format("{0} number(s) remain.", foo.CountRemaining.ToString("n0"))
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
'start over using the same list
foo.Restart()
lb1Upd()
ListBox2.Items.Clear()
End Sub
Private Sub lb1Upd()
'update not selected listbox
ListBox1.Items.Clear()
ListBox1.Items.AddRange(foo.ListNotSelected.ToArray)
End Sub
Private Sub Form1_Shown(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Shown
TextBox1.Text = "10"
TextBox2.Text = "1"
End Sub
End Class
Class CommonRandomQ
Private Shared _prng As New Random
Private _list As List(Of String)
Private _working As List(Of String)
Public Sub New(ByVal High As Integer) '0-high inclusive
Me.New(0, High)
End Sub
Public Sub New(ByVal Low As Integer, ByVal High As Integer) 'low-high inclusive
Me._list = New List(Of String)
For x As Integer = Low To High
Me._list.Add(x.ToString)
Next
Me.Restart()
End Sub
Public Sub Restart() 're-use same original list
Me._working = New List(Of String)
Me._working.AddRange(Me._list)
End Sub
Public Function ListNotSelected() As List(Of String)
Return Me._working
End Function
Public Function NextRandom() As String
Dim rv As String = Nothing
If Me._working.Count > 0 Then
Dim idx As Integer = _prng.Next(0, Me._working.Count)
rv = Me._working(idx)
Me._working.RemoveAt(idx)
End If
Return rv
End Function
Public ReadOnly Property CountRemaining() As Integer
Get
Return Me._working.Count
End Get
End Property
End Class
-
Nov 30th, 2010, 03:16 PM
#19
Thread Starter
Addicted Member
Re: Random Number Generator - Store generated numbers
I understand what you guys are saying but its working now! All I need to do is get the output to the textbox and its complete.. So why would I want to start again?
Its generating a random number between user inputted values, its not generating a new list every time and its storing the generated numbers in the listbox.
All I now need is for the latest generated number to be placed into the Textbox3 on my form1!
-
Nov 30th, 2010, 03:49 PM
#20
Thread Starter
Addicted Member
Re: Random Number Generator - Store generated numbers
Ok so I fumbled my way to getting it working. I just swapped the order I was trying to do things and that seemed to work.
I was trying to go generate to listbox, then display in textbox.
The proper way was generate to textbox, and then add to listbox.
vb Code:
If numbers.Count > 0 Then
Dim rnd As New Random
Dim idx As Integer = rnd.Next(0, numbers.Count)
'ListBox1.Items.Add(numbers.Item(idx))
TextBox3.Text = (numbers.Item(idx))
numbers.RemoveAt(idx)
ListBox1.Items.Add(TextBox3.Text)
Else
MessageBox.Show("There are no more numbers left to generate!")
End If
Form3.Show()
tmrWait.Interval = 5000
tmrWait.Start()
Everything all go, I guess the real last thing to do is to safe it all up.
When no numbers are entered and they press generate it will crash kinda thing. No ideas on that?
-
Nov 30th, 2010, 04:01 PM
#21
Thread Starter
Addicted Member
Re: Random Number Generator - Store generated numbers
Added this as a start.. Surprised it worked first Pop
vb Code:
If TextBox2.Text > TextBox1.Text Then MessageBox.Show("Your high value is lower than your low value!") Else
Question about that is, should I do it that way you have the message box appear in the else part?
Also added these. Now I am adding all of this to my generate button. Should this be placed here or is there a secrete sub you could put it in, and I guess call it later.
vb Code:
If TextBox1.Text = Nothing Then MessageBox.Show("Please enter a High Value!") Else If TextBox2.Text = Nothing Then MessageBox.Show("Please enter a Low Value!") Else
Last edited by Noob script kiddie; Nov 30th, 2010 at 04:09 PM.
-
Nov 30th, 2010, 04:10 PM
#22
Re: Random Number Generator - Store generated numbers
 Originally Posted by Noob script kiddie
Added this as a start.. Surprised it worked first Pop
vb Code:
If TextBox2.Text > TextBox1.Text Then
MessageBox.Show("Your high value is lower than your low value!")
Else
Question about that is, should I do it that way you have the message box appear in the else part?
If you look at some of the other examples, you will see the correct way to handle the conversion from text to numbers. Your comparison may appear to work but it isn't how it should be done.
-
Nov 30th, 2010, 05:12 PM
#23
Frenzied Member
Re: Random Number Generator - Store generated numbers
at this point he has not tested for alpha inputs so his test will work fine numbers as literals or some kind of numeric datatype all test the same in textboxes
the empty and alphabetic tests should prehaps happen before the simplistic text1.text>text2.text expressions though for completness
the kids getting there though...
but it is always good to restart your solution as you are able to utilise new concepts that make for a more robust and/or faster solution, sometimes even prettier and smaller/easier to read.
"programming is more of an art than a science"
-
Nov 30th, 2010, 05:27 PM
#24
Thread Starter
Addicted Member
Re: Random Number Generator - Store generated numbers
You guys get me worried when you talk about me 
What are Alpha inputs?
What is conversion from text to numbers? I don't understand what your referring to..
-
Nov 30th, 2010, 05:38 PM
#25
Re: Random Number Generator - Store generated numbers
 Originally Posted by incidentals
at this point he has not tested for alpha inputs so his test will work fine numbers as literals or some kind of numeric datatype all test the same in textboxes
the empty and alphabetic tests should prehaps happen before the simplistic text1.text>text2.text expressions though for completness
the kids getting there though...
but it is always good to restart your solution as you are able to utilise new concepts that make for a more robust and/or faster solution, sometimes even prettier and smaller/easier to read.
"programming is more of an art than a science"
If you are saying that as long as the user inputs only numbers the code will work, then yes. We all know that users never make mistakes 
@noob - look at the code I posted. TryParse is something you should search for here http://msdn.microsoft.com/en-us/libr...VS.100%29.aspx.
-
Dec 1st, 2010, 05:09 PM
#26
Thread Starter
Addicted Member
Re: Random Number Generator - Store generated numbers
 Originally Posted by dbasnett
If you are saying that as long as the user inputs only numbers the code will work, then yes. We all know that users never make mistakes
@noob - look at the code I posted. TryParse is something you should search for here http://msdn.microsoft.com/en-us/libr...VS.100%29.aspx.
Thanks, I looked at this code before but didn't want to start again. However I got to work this morning and just figured why not.. Set it up and its much nicer. I have incorporated my side of things to it and its working well. I like the fact that it picks up on non alphanumeric (thanks incidentals)
Hope your happy with me using it 
The only problem now I'm having is with the timer I'm using, however that is a different thread so I will take that there.
http://www.vbforums.com/showthread.php?t=633869
-
Nov 30th, 2010, 07:38 PM
#27
Frenzied Member
Re: Random Number Generator - Store generated numbers
we like to test everything
alpha - alphabetic/alphanumeric a mix of letters and numbers and spaces and stuff
we like to findout what breaks our programs and then prevent it
-
Dec 6th, 2010, 03:37 PM
#28
Thread Starter
Addicted Member
Re: Random Number Generator - Store generated numbers
Another question, is there a way to have a button that removes a selected item in the Generated list from the virtual list of numbers? So they could effectively delete certain numbers from being generated?
-
Dec 6th, 2010, 09:45 PM
#29
Re: Random Number Generator - Store generated numbers
 Originally Posted by Noob script kiddie
Another question, is there a way to have a button that removes a selected item in the Generated list from the virtual list of numbers? So they could effectively delete certain numbers from being generated?
Most of the implementations start by generating a collection of the items to return by doing Enumerable.Range(0, numberOfItems).ToList (or .ToArray)
The Enumerable.Range() method will return a sequence of increasing integers. The ToX() method collapses that sequence into the collection. So simply replacing that with a collection that just contains the elements you want returned will do what you want.
-
Dec 6th, 2010, 06:27 PM
#30
Re: Random Number Generator - Store generated numbers
This topic has just been covered on the Visual Basic site of About.com whic hare linked to from the VB News box at the top of the forum.
The link you'd be wanting is Collections of Random Integers - Redux
-
Dec 7th, 2010, 09:46 AM
#31
Re: Random Number Generator - Store generated numbers
@evil - The implementation of a specific method always depends on the specifics, and balancing performance with maintainability.
If the task is to simulate one of the popular lottery games and display the results to the user then using a list is probably best. If the task is to simulate 10,000,000 draws and track the results, then a list might not be the best approach.
What do you do if the task is to pick random integers(without repeats) from {-17179869184 to 17179869184} inclusive? I don't think list or arrays will work out well, do you?
Last edited by dbasnett; Dec 8th, 2010 at 07:22 AM.
-
Dec 7th, 2010, 07:31 PM
#32
Re: Random Number Generator - Store generated numbers
 Originally Posted by dbasnett
What do you do if the task is to pick random integers(without repeats) from {-17,179,869,184 , 17,179,869,184}? I don't think list or arrays will work out well, do you?
Seems to work okay to me, maybe I'm misunderstanding you:
Code:
Array: | # | |
0 1 2 3 4 5 6 7 | Rem | Rnd | Returned
---------------------------------+-----+-----+----------
-17 179 869 184 17 179 869 184 | 8 | 6 | 869
-17 179 869 184 17 179 184 184 | 7 | 6 | 184
-17 179 869 184 17 179 184 184 | 6 | 0 | -17
179 179 869 184 17 179 184 184 | 5 | 3 | 184
179 179 869 17 17 179 184 184 | 4 | 3 | 17
179 179 869 17 17 179 184 184 | 3 | 0 | 179
869 179 869 17 17 179 184 184 | 2 | 0 | 869
179 179 869 17 17 179 184 184 | 1 | 0 | 179
I'm assuming that "without repeats" recognises the doubled up elements as two separate elements? If you meant that it should only return one of those elements, then you simply make sure your source set doesn't contain duplicates, which is a trivial extension, or you pick option 3. Neither implementation of option 1 would handle that question without modification. But as I wasn't suggesting option 1 over option 3, I'm not sure what your point is in bringing that up?
As to balancing performance and maintainability, *shrug* in this instance we're not talking about massive amounts of time either way, although a rough performance measure puts the Array method at over twice as fast as the List method.
But the array is the natural data structure for the operations we're performing in algorithm 1, namely index based access. The List can do it, of course, and allows for using the RemoveAt method to let the code speak more clearly, but given that we should be encapsulating the Randomise inside it's own method (and also, for my money, class) then we're talking about 5 lines of code, inside a low level method, I don't value that additional immediate clarity overly. It really doesn't take long to grasp what the array method is doing when it stands on its own.
Plus, if you want ultra maintainable code, without worrying particularly about performance, you should be using option 2.
With the difference between the List and HashSet in algorithm 3, this really is important, and doesn't change the readability of the algorithm at all (you'd only change the declaration of the instance, I believe the methods you'd use are all named the same, from memory)
So, no, I don't believe my advice is particularly context specific, as I was just giving some additional advice on the implementation of each of the algorithms, not suggesting a particular one over the other. I was assuming the correct approach had been picked for the OP's scenario and offering some additional insight into that approach.
-
Dec 8th, 2010, 12:14 AM
#33
Re: Random Number Generator - Store generated numbers
[QUOTE=dbasnett;3923815{-17,179,869,184 , 17,179,869,184[/QUOTE]
was meant to be -17 179 869 184 up to 17 179 869 184 .
dbasnett used to "," to seperate thousands, millions etc.
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Dec 8th, 2010, 12:35 AM
#34
-
Dec 8th, 2010, 07:23 AM
#35
Re: Random Number Generator - Store generated numbers
 Originally Posted by opus
was meant to be -17 179 869 184 up to 17 179 869 184 .
dbasnett used to "," to seperate thousands, millions etc. 
I fixed the post in place.
@evil - my point was that you pick the tool that best fits the job.
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
|