0

I have code for a randomizer that puts a random image into 2 picture boxes but on one of them the picture stays the same and does not change. Here is my code.

Dim Random As Integer
Dim Random2 As Integer

Random = CInt(Math.Ceiling(Rnd() * 6)) + 0
Random = CInt(Math.Ceiling(Rnd() * 6)) + 0

If Random = 1 Then
    PictureBox1.Image = Image.FromFile("C:\Users\sahib\Desktop\another rounders -_-\another rounders -_-\bin\Dice Faces\Dice1.png")
ElseIf Random = 2 Then
    PictureBox1.Image = Image.FromFile("C:\Users\sahib\Desktop\another rounders -_-\another rounders -_-\bin\Dice Faces\Dice2.png")

I did this up to six then started again but this time with Random2 As Integer and PictureBox2 (the one that doesn't change image). I am very confused why this is happening.

Blackwood
  • 4,504
  • 16
  • 32
  • 41
Sahib
  • 1
  • 1
  • Does this help? https://stackoverflow.com/a/34644615/945456 – Jeff B Dec 10 '18 at 22:43
  • 1
    If you are going to use `Rnd` then you should call `Randomize` but you should only do it once, which that linked answer doesn't stipulate. That said, you should use the `Random` class in VB.NET, which is included in an answer in that thread but not the accepted one. – jmcilhinney Dec 10 '18 at 22:58
  • You are not showing enough code to diagnose the issue. Please show enough so that anyone can recreate the problem. – MarkL Dec 10 '18 at 23:45
  • `Random=CInt(Math.Ceiling(Rnd() * 6)) + 0`,`Random2=CInt(Math.Ceiling(Rnd() * 6)) + 0`. The `Random` is for `PictureBox1` and `Random2` is for `PictureBox2` – kiLLua Dec 11 '18 at 01:13

1 Answers1

1

Big EDIT thanks to @Jimi.

First use the .net Random class. It is easier to use. Then in the Solution Explorer, add a new folder named Images. Then right click and add the images to the folder. You need to select all the files you added and the choose a Build Action -> Additional Files and Copy to Output Directory -> Copy if newer.

'Form level (class scope) can be seen by all methods
 Private Dice As New List(Of String)
 Private Rand As New Random

'Fill the Dice list just once in Form.Load
 Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For i = 1 To 6
        Dice.Add($"Images\Dice{i}.png")
    Next
End Sub

Private Sub RollDice()
    PictureBox1.Image = Image.FromFile(Dice(Rand.Next(1, 7)))
    PictureBox2.Image = Image.FromFile(Dice(Rand.Next(1, 7)))
End Sub   
Mary
  • 14,926
  • 3
  • 18
  • 27