Items from array to textboxes
Hello to the community.
I am using vb 2010 an I am dealing with a problem. I have an array of values and some textboxes
Array has some values like these
Dim numbers() as Integer= {1,7,2,4}
I am trying to put these values to 4 textboxes in a random way.
How can I do that?
Re: Items from array to textboxes
Any time you want to do something random, you need to generate a random number and then use that in whatever way is appropriate for your scenario. You can shuffle a list by sorting it on a random number and then take the items onbe by one, much like dealing cards. In your case, you could shuffle the numbers or the TextBoxes, e.g.
vb.net Code:
Dim rng As New Random
Dim numbers = {1,7,2,4}
Dim textBoxes = {TextBox1, TextBox2, TextBox3, TextBox4}.OrderBy(Function(tb) rng.NextDouble()).ToArray()
For i = 0 To numbers.GetUpperBound(0)
textBoxes(i).Text = numbers(i).ToString()
Next
Re: Items from array to textboxes
Thanks for your help.
I have managed somehow to make it work like this
Code:
Dim nums() As Double = {10,20,30,40,50}
Dim rnd As New Random()
Dim shufflenums = nums.OrderBy(Function() rnd.Next).ToArray()
For Me.k = 0 To nums.GetUpperBound(0)
mytextbox(k).text = shufflenums(k).ToString
Next k
The problem is that some values are be repeated....
Re: Items from array to textboxes
Quote:
Originally Posted by
stratos
The problem is that some values are be repeated....
I don't see how that's possible, if you have populated mytextbox properly. We can only guess that you've done that though. Have you actually debugged the code properly, using a breakpoint and stepping through it line by line? If you do that, which you should have done before posting here, then you'd be able to see what the contents of shufflenums and shufflenums(k) is so you'd be able to see whether there actually is a number duplicated and why. VS has a debugger for a reason. You need to use it to find and fix bugs like this.
Re: Items from array to textboxes
Does he not mean that you can get e.g. 1,7,7,4? (the 7 repeated)
I don't know if it can happen, I'm just asking... Because it's still a random number
Re: Items from array to textboxes
If he does mean that, it wouldn't make sense because those random values are only used to trick LINQ into randomly ordering the list. They aren't used for anything else and even if they do repeat, it wouldn't reduce the effectiveness of it's use.
Re: Items from array to textboxes
Quote:
Originally Posted by
schoemr
Does he not mean that you can get e.g. 1,7,7,4? (the 7 repeated)
I don't know if it can happen, I'm just asking... Because it's still a random number
It's possible, but Next will generate a value in the range 0 to Integer.MaxValue-1, so it's unlikely. As Niya said though, that wouldn't matter as it would just mean that the corresponding elements from the original array had equal priority within the sort. I'm pretty sure that the algorithm used in that scenario is unstable anyway, so you still wouldn't know whether those two values would appear in the original order or not.
Re: Items from array to textboxes
Thank you guys for the support.
I went through the code and I found what was wrong.
I think shuffle code works as intended. I have messed up with some textboxes indices (some of them were the same), that's why they displayed the same values.
So far, I have tested it and never got same numbers again. Array shuffles like a charm