Support Forums

Full Version: Need help with generating random fixtures
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have 2 ListBoxes and a TextBox.
Each ListBox contains usernames which are manually added by the user.
I'd like random fixtures to be generated into the TextBox.

For example:

ListBox1:
Tom
Fred
Harry

ListBox2:
John
Dave
Bill

When the user hits generate, I'd like the fixtures to be randomly generated in the TextBox like so:

Tom vs Dave
Fred vs Bill
Harry vs John


Anybody help me out with this? I haven't coded in ages and I'm very rusty.

I supose you're going to use an array for the inputted strings?
I'm not 100% sure, haven't coded in vb.net for ages. It's all java now.
I would know how to do it with the strings already there but not if you don't know what they are.

You could ask them to input the amount of strings they are going to insert and hold it in a variable such as ins then do
Dim num As Integer = random.Next(ins)

In java if I used an array I would just do array.length to find how many elements are in the array instead of asking them to input how many they are going to insert.
Not sure how to do that in vb.net though think you need to use upperbounds method.
Here is the code for the generate button:
Code:
Dim r As New Random
TextBox1.Text = String.Format("{0} vs {1}", ListBox1.Items(r.Next(0, ListBox1.Items.Count)), ListBox2.Items(r.Next(0, ListBox2.Items.Count)))
(03-14-2011, 10:28 AM)thanasis2028 Wrote: [ -> ]Here is the code for the generate button:
Code:
Dim r As New Random
TextBox1.Text = String.Format("{0} vs {1}", ListBox1.Items(r.Next(0, ListBox1.Items.Count)), ListBox2.Items(r.Next(0, ListBox2.Items.Count)))

Thank you that works, but it only generates 1 fixture. I need each player to have a random fixture.

Thanks.

You need to create a dynamic array.
Get the length of the array then generate the random number as I showed.
Then tell it to put the element of the array into the textbox.

The only problem would be it may put the same username more than once. I'm not sure if it does though.
You will have to use the code i gave you many times but changing the line:
Code:
TextBox1.Text = String.Format("{0} vs {1}", ListBox1.Items(r.Next(0, ListBox1.Items.Count)), ListBox2.Items(r.Next(0, ListBox2.Items.Count)))
with
Code:
TextBox1.AppendText(String.Format("{0} vs {1}", ListBox1.Items(r.Next(0, ListBox1.Items.Count)), ListBox2.Items(r.Next(0, ListBox2.Items.Count))))
You have to find a way to prevent the same name from appearing in the list again. This might be a little hard...
(03-15-2011, 12:17 PM)thanasis2028 Wrote: [ -> ]You will have to use the code i gave you many times but changing the line:
Code:
TextBox1.Text = String.Format("{0} vs {1}", ListBox1.Items(r.Next(0, ListBox1.Items.Count)), ListBox2.Items(r.Next(0, ListBox2.Items.Count)))
with
Code:
TextBox1.AppendText(String.Format("{0} vs {1}", ListBox1.Items(r.Next(0, ListBox1.Items.Count)), ListBox2.Items(r.Next(0, ListBox2.Items.Count))))
You have to find a way to prevent the same name from appearing in the list again. This might be a little hard...

Thanks but I think what Untouch suggested is a much more reliable solution.