MayClem

hi,

very new to this - only started with vb a week ago!

my boss wanted a random generator to decide who's turn it was to make the tea - i know, a silly thing really.

essentially all i need is a way to randomly pick a name from a list of 5 or 6 people, do i do this in the same kind of way as a random number

any help would be appreciated

Thanks



Re: Visual Basic Express Edition random name generator

cardboardtoast

Yes a random number generator would do.

here's my example(if you don't want it don't look past here =0) just change the names, or change the intUpperNumber to the number you want

Public Class Form1

Private Sub btnGetName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetName.Click

Dim intUpperNumber As Integer = 6

Dim r As New Random

Dim intRandomnumber

intRandomnumber = r.Next(intUpperNumber)

Select Case intRandomnumber

Case 0

TextBox1.Text = "Tom"

Case 1

TextBox1.Text = "Fred"

Case 2

TextBox1.Text = "Ted"

Case 3

TextBox1.Text = "Daniel"

Case 4

TextBox1.Text = "Alex"

Case 5

TextBox1.Text = "Rob"

End Select

End Sub

End Class





Re: Visual Basic Express Edition random name generator

Solitaire

Here is how you can make a list of 6 names in random order without repeating any of the names. The names can be entered once and the list repeated for a different assortment after all six names have had their turn. Place a listbox on the form to hold the list of 6 names, and a button to start running the code.

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim mix, reply As Integer, temp As String

Dim shuffle(6) As String

Dim rnd As New Random(6)

For x As Integer = 0 To 5

shuffle(x) = InputBox("Enter name # " & (x + 1).ToString)

Next x

Do

For x As Integer = 0 To 5 'swap array positions randomly

mix = rnd.Next(0, 6)

temp = shuffle(mix)

shuffle(mix) = shuffle(x)

shuffle(x) = temp

Next x

ListBox1.Items.Clear()

For x As Integer = 0 To 5

ListBox1.Items.Add((shuffle(x)))

Next x

reply = MessageBox.Show("Press OK to repeat with a new order, Cancel to stop.", "", MessageBoxButtons.OKCancel)

Loop Until reply = 2

End Sub






Re: Visual Basic Express Edition random name generator

MayClem

Thanks - will try that when i get home tonight solitaire Smile

the solution from mr toast seemed to work - but never hurts to have extra ways to achieve things

already glad i found this forum - vb started giving me a headache when i just dived in from scratch!





Re: Visual Basic Express Edition random name generator

Solitaire

The code given by cardboardtoast will likely repeat the same name before going through all the names in the list, so it's not fair to that person, and not the best solution for you.

The code I gave you is guaranteed not to repeat the same name or leave any name out.






Re: Visual Basic Express Edition random name generator

JohnWein

I think you simply want a random name from a list of names. If a particular name seems to come up more frequently than another, that adds interest to the process. This code works the same as cardboardtoast's with less typing:

Code Snippet

Public Class Form1

Private Private TeaMakers() As String = {"Tom", "Fred", "Ted", "Daniel", "Alex", "Rob"}

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Randomize()

TextBox1.Text = "It's " & TeaMakers(CInt(Fix(6 * Rnd()))) & "'s turn to make the tea."

End Sub

End Class

Solitaire's code simply rearranges the list of names. If you enter the names in the same order each time, the output order is the same each time.





Re: Visual Basic Express Edition random name generator

ReneeC

The word "random" is not being used correctly here. "Random" does not shuffle and random does repeat qualities of random which are being dispensed with.






Re: Visual Basic Express Edition random name generator

Solitaire

My mistake. Please forgive me.

I had inadvertantly added a seed argument to the Random method. That caused the same sequence to repeat every time the program was restarted. To correct, please replace the Dim statement with this one:

Dim rnd As Random = New Random()

The complete correct code is as follows:

Code Snippet

Dim mix, reply As Integer, temp As String

Dim shuffle(6) As String

Dim rnd As Random = New Random()

For x As Integer = 0 To 5

shuffle(x) = InputBox("Enter name # " & (x + 1).ToString)

Next x

Do

For x As Integer = 0 To 5 'swap array positions randomly

mix = rnd.Next(0, 6)

temp = shuffle(mix)

shuffle(mix) = shuffle(x)

shuffle(x) = temp

Next x

ListBox1.Items.Clear()

For x As Integer = 0 To 5

ListBox1.Items.Add((shuffle(x)))

Next x

reply = MessageBox.Show("Press OK to repeat with a new order, Cancel to stop.", "", MessageBoxButtons.OKCancel)

Loop Until reply = 2

By the way, the OP didn't specify whether or not it was OK for the same person to be selected randomly twice before everyone else had a chance. I assumed it wouldn't be fair to do it that way, and to simply set up a schedule that would be repeated randomly in a different order after everyone had a chance.






Re: Visual Basic Express Edition random name generator

MayClem

Thanks to you all - as i said, i'm still very new to all this, but learning fast! Smile

i appreciate all your help - and yes, probably wouldn't be fair to have the same person coming up twice (especially as they might be the one that makes lousy tea!).

looks like i'll be busy tonight...





Re: Visual Basic Express Edition random name generator

JohnWein

You want a schedule, not a random selection.



Re: Visual Basic Express Edition random name generator

Derek Smyth

If you've just started shouldn't it be you making the tea.

Smile

This post is an excellent example of taking something simple and making it complex.

We have all done it before and we will all do it again.

Code Snippet

Dim teaMakers() As String = {"You", "Me", "Him", "Her", "The other one"}

Dim maker As Int32 = New Random().Next(0, teaMakers.Length)

Console.WriteLine(teaMakers(maker))

Console.ReadLine()

You could even load the names from a text file (one tea maker per line).

Code Snippet
Dim
teaMakers() As String = System.IO.File.ReadAllLines("teaMakers.txt")

And if you had a tea break every 10 minutes, like we do, then use a timer.

Code Snippet

Sub Main()

Dim teaMakers() As String = {"You", "Me", "Him", "Her", "The other one"}

Dim tmer As New Threading.Timer(AddressOf MakeTheTea, teaMakers, 0, 1000)

Console.ReadLine()

End Sub

Private Sub MakeTheTea(ByVal state As Object)

Dim teaMakers() As String = state

Dim maker As Int32 = New Random().Next(0, teaMakers.Length)

Console.WriteLine(teaMakers(maker))

End Sub

This goes off every second (1000 milliseconds)...

I read your new to VB and some very helpful people have posted some code for you to look at but hopefully this give you some more ideas. This was a nice break from all the other heavy programming questions that get posted.






Re: Visual Basic Express Edition random name generator

CBateson_ITTstudent

I'm experimenting witha variation of this code and I'm having trouble writing a correct If statement, that when the user hits cancel the Input box stops and the randomized name is then displayed.. I got the if statment to stop the input boxes, but I can not get it to finish the randomizing.

Any suggestions

Code Snippet

'For... Next Loop to enter student names until cancel is selected,

For x As Integer = 0 To 5

students(x) = InputBox("Enter Student " & (x + 1).ToString & " Name.", "Random Perfect Attendance Winner")

If students(x) = "" Then

Exit For

Else

End If

Next x

For x As Integer = 0 To 5 'swap array positions randomly

mix = rnd.Next(0, 5)

temp = students(mix)

students(mix) = students(x)

students(x) = temp

Next x





Re: Visual Basic Express Edition random name generator

Solitaire

Please start a new thread. This has nothing to do with the tea-making problem. There are a number of issues with your code, but they should not be addressed in this thread.