Reputation: 1
Partial Class Default2
Inherits System.Web.UI.Page
Private Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
Dim Flagimageurl(6) As String ' creating the flag image url array'
Flagimageurl(0) = "https://www.cia.gov/library/publications/the-world-factbook/graphics/flags/large/uk-lgflag.gif"
Flagimageurl(1) = "https://upload.wikimedia.org/wikipedia/en/thumb/c/c3/Flag_of_France.svg/800px-Flag_of_France.svg.png"
Flagimageurl(2) = "https://upload.wikimedia.org/wikipedia/en/thumb/0/05/Flag_of_Brazil.svg/720px-Flag_of_Brazil.svg.png"
Flagimageurl(3) = "https://upload.wikimedia.org/wikipedia/en/thumb/9/9a/Flag_of_Spain.svg/750px-Flag_of_Spain.svg.png"
Flagimageurl(4) = "https://upload.wikimedia.org/wikipedia/en/thumb/a/a4/Flag_of_the_United_States.svg/1235px-Flag_of_the_United_States.svg.png"
Flagimageurl(5) = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a2/Flag_of_Russia_%28Kremlin.ru%29.svg/1024px-Flag_of_Russia_%28Kremlin.ru%29.svg.png"
Dim CountryNames(6) As String 'creating the country names array'
CountryNames(0) = "United Kingdom"
CountryNames(1) = "France"
CountryNames(2) = "Brazil"
CountryNames(3) = "Spain"
CountryNames(4) = "United States of America (USA)"
CountryNames(5) = "Russia"
flag.ImageUrl = Flagimageurl(6 * Rnd()) 'Choosing a random flag'
RadioButton1.Text = CountryNames(5 * Rnd()) 'Randomly picks the country according to the CountryNames array'
RadioButton2.Text = CountryNames(5 * Rnd()) ' with random * 6 you get blanks so you need to use random * 5'
RadioButton3.Text = CountryNames(5 * Rnd())
RadioButton4.Text = CountryNames(5 * Rnd())
If RadioButton1.Text = RadioButton2.Text Then 'Makes sure that the radiobuttons don't show duplicate answers'
RadioButton2.Text = CountryNames(5 * Rnd())
End If
If RadioButton2.Text = RadioButton1.Text Then
RadioButton1.Text = CountryNames(5 * Rnd())
End If
If RadioButton3.Text = RadioButton1.Text Or RadioButton3.Text = RadioButton2.Text Then
RadioButton3.Text = CountryNames(5 * Rnd())
End If
If RadioButton4.Text = RadioButton1.Text Or RadioButton2.Text = RadioButton3.Text Then
RadioButton4.Text = CountryNames(5 * Rnd())
End If
End Sub
End Class
The problem is that when I run the code two of the four answers are the same. Or the other thing that it does is it does not always have the correct answer for that flag.
How can I fix this issue?
Upvotes: 0
Views: 138
Reputation: 4439
One way of doing it is to replace all your If
statements with the 3 loops below - not the best way of doing it, but for the purposes of your program, it won't impact performance much.
The code checks that buttons 2,3 and 4 are not the same as any other. You dont need to check button 1 because the loops will check that all of the subsequent buttons aren't the same as it.
Each loop will choose a new random text string from your array. If it is the same as any of the other buttons, it will choose another one until it is different to all of them.
Do While RadioButton2.Text = RadioButton1.Text Or RadioButton2.Text = RadioButton3.Text Or RadioButton2.Text = RadioButton4.Text 'Makes sure that the radiobuttons don't show duplicate answers'
RadioButton1.Text = CountryNames(5 * Rnd())
Loop
Do While RadioButton3.Text = RadioButton2.Text Or RadioButton3.Text = RadioButton1.Text Or RadioButton3.Text = RadioButton4.Text 'Makes sure that the radiobuttons don't show duplicate answers'
RadioButton1.Text = CountryNames(5 * Rnd())
Loop
Do While RadioButton4.Text = RadioButton2.Text Or RadioButton4.Text = RadioButton3.Text Or RadioButton4.Text = RadioButton1.Text 'Makes sure that the radiobuttons don't show duplicate answers'
RadioButton1.Text = CountryNames(5 * Rnd())
Loop
However, your choice of using Rnd()
will be very unlikely to generate the number 5. You would be better changing the above code to this
Dim randomNumber As New Random
flag.ImageUrl = Flagimageurl(randomNumber.Next(0, 5)) 'Choosing a random flag'
RadioButton1.Text = CountryNames(randomNumber.Next(0, 5)) 'Randomly picks the country according to the CountryNames array'
RadioButton2.Text = CountryNames(randomNumber.Next(0, 5))
RadioButton3.Text = CountryNames(randomNumber.Next(0, 5))
RadioButton4.Text = CountryNames(randomNumber.Next(0, 5))
Do While RadioButton2.Text = RadioButton1.Text Or RadioButton2.Text = RadioButton3.Text Or RadioButton2.Text = RadioButton4.Text 'Makes sure that the radiobuttons don't show duplicate answers'
RadioButton1.Text = CountryNames(randomNumber.Next(0, 5))
Loop
Do While RadioButton3.Text = RadioButton2.Text Or RadioButton3.Text = RadioButton1.Text Or RadioButton3.Text = RadioButton4.Text 'Makes sure that the radiobuttons don't show duplicate answers'
RadioButton1.Text = CountryNames(randomNumber.Next(0, 5))
Loop
Do While RadioButton4.Text = RadioButton2.Text Or RadioButton4.Text = RadioButton3.Text Or RadioButton4.Text = RadioButton1.Text 'Makes sure that the radiobuttons don't show duplicate answers'
RadioButton1.Text = CountryNames(randomNumber.Next(0, 5))
Loop
This code will be equally likely to generate all of the numbers.
Upvotes: 0
Reputation: 4489
First I would consider using a Dictionary instead:
Dim countries As New Dictionary(Of String, String) _
From {{"United Kingdom", "https://www.cia.gov/library/publications/the-world-factbook/graphics/flags/large/uk-lgflag.gif"},
{"France", "https://upload.wikimedia.org/wikipedia/en/thumb/c/c3/Flag_of_France.svg/800px-Flag_of_France.svg.png"},
{"Brazil", "https://upload.wikimedia.org/wikipedia/en/thumb/0/05/Flag_of_Brazil.svg/720px-Flag_of_Brazil.svg.png"},
{"Spain", "https://upload.wikimedia.org/wikipedia/en/thumb/9/9a/Flag_of_Spain.svg/750px-Flag_of_Spain.svg.png"},
{"United States of America (USA)", "https://upload.wikimedia.org/wikipedia/en/thumb/a/a4/Flag_of_the_United_States.svg/1235px-Flag_of_the_United_States.svg.png"},
{"Russia", "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a2/Flag_of_Russia_%28Kremlin.ru%29.svg/1024px-Flag_of_Russia_%28Kremlin.ru%29.svg.png"}}
I would then use the Random class, add the item to another Dictionary
and remove the item from the current Dictionary
so it cannot be used again. I'm also looping through the six RadioButton
controls to reduce code but you could separate out and do each RadioButton
in turn. This would depend on your design:
Dim countriesSelected As New Dictionary(Of String, String)
Dim countryRandom As New Random
For Each rd As RadioButton In Me.Controls.OfType(Of RadioButton)()
Dim nextRandom As Integer = countryRandom.Next(countries.Count)
rd.Text = countries.Keys(nextRandom)
countriesSelected.Add(countries.Keys(nextRandom), countries.Values(nextRandom))
countries.Remove(countries.Keys(nextRandom))
Next
Dim flagRandom As New Random
flag.ImageUrl = countriesSelected.Values(flagRandom.Next(countriesSelected.Count))
Output:
For if you want to separate out here is the code:
Dim countryRandom As New Random
Dim nextRandom As Integer = countryRandom.Next(countries.Count)
RadioButton1.Text = countries.Keys(nextRandom)
countriesSelected.Add(countries.Keys(nextRandom), countries.Values(nextRandom))
countries.Remove(countries.Keys(nextRandom))
nextRandom = countryRandom.Next(countries.Count)
RadioButton2.Text = countries.Keys(nextRandom)
countriesSelected.Add(countries.Keys(nextRandom), countries.Values(nextRandom))
countries.Remove(countries.Keys(nextRandom))
nextRandom = countryRandom.Next(countries.Count)
RadioButton3.Text = countries.Keys(nextRandom)
countriesSelected.Add(countries.Keys(nextRandom), countries.Values(nextRandom))
countries.Remove(countries.Keys(nextRandom))
nextRandom = countryRandom.Next(countries.Count)
RadioButton4.Text = countries.Keys(nextRandom)
countriesSelected.Add(countries.Keys(nextRandom), countries.Values(nextRandom))
countries.Remove(countries.Keys(nextRandom))
You can see why I chose to loop through the RadioButton
controls. It's much simpler to read and maintain.
If you really must use an Array
as you've said in your comments then I'll try and do that for you. The code is ugly and probably harder to understand, at least in my opinion but it should give you what you are after.
First change (6)
to (5)
as this will create 6 items. An Array
is zero based so 0 is the first item not 1. The indexes in both of the arrays must be identical so that we pick the right flag at the end.
Dim Flagimageurl(5) As String ' creating the flag image url array'
Flagimageurl(0) = "https://www.cia.gov/library/publications/the-world-factbook/graphics/flags/large/uk-lgflag.gif"
Flagimageurl(1) = "https://upload.wikimedia.org/wikipedia/en/thumb/c/c3/Flag_of_France.svg/800px-Flag_of_France.svg.png"
Flagimageurl(2) = "https://upload.wikimedia.org/wikipedia/en/thumb/0/05/Flag_of_Brazil.svg/720px-Flag_of_Brazil.svg.png"
Flagimageurl(3) = "https://upload.wikimedia.org/wikipedia/en/thumb/9/9a/Flag_of_Spain.svg/750px-Flag_of_Spain.svg.png"
Flagimageurl(4) = "https://upload.wikimedia.org/wikipedia/en/thumb/a/a4/Flag_of_the_United_States.svg/1235px-Flag_of_the_United_States.svg.png"
Flagimageurl(5) = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a2/Flag_of_Russia_%28Kremlin.ru%29.svg/1024px-Flag_of_Russia_%28Kremlin.ru%29.svg.png"
Dim CountryNames(5) As String 'creating the country names array'
CountryNames(0) = "United Kingdom"
CountryNames(1) = "France"
CountryNames(2) = "Brazil"
CountryNames(3) = "Spain"
CountryNames(4) = "United States of America (USA)"
CountryNames(5) = "Russia"
I have created a separate Array
in an attempt to hold the countries that have been selected.
Dim selectedCountries(3) As String
Still use the Random
class and loop through the RadioButton
controls only this time we have to also loop through selectedCountries
to ensure we don't get duplicates. I initilise addCountry
with a value of False
, set it to True
after I've initialised my loop and then set to False
again if I find that the country has already been selected. This will then continue to loop until a new country has been selected.
Dim countryRandom As New Random
Dim i As Integer = 0
For Each rb As RadioButton In Me.Controls.OfType(Of RadioButton)()
Dim randomNumber As Integer
Dim addCountry As Boolean = False
Do While addCountry = False
randomNumber = countryRandom.Next(CountryNames.Count)
addCountry = True
For Each selectedCountry In selectedCountries
If selectedCountry = randomNumber Then
addCountry = False
Exit For
End If
Next
Loop
rb.Text = CountryNames(randomNumber)
selectedCountries(i) = randomNumber
i += 1
Next
I then sort the Array
:
Array.Sort(selectedCountries)
I look at getting a random number then checking to see if that random number is in the selectedCountries
Array
. I then use that:
Dim flagRandom As New Random
Dim selectedFlag As String = ""
Do While selectedFlag = ""
Dim randomNumber As Integer = flagRandom.Next(Flagimageurl.Count)
For Each selectedCountry In selectedCountries
If selectedCountry = randomNumber Then
selectedFlag = Flagimageurl(randomNumber)
End If
Next
Loop
flag.ImageUrl = selectedFlag
This should give you what you are after but I cannot stress how inefficient this code actually is.
Upvotes: 4