Random is not being to random - vb.net

Public Class Form1
Dim num1 As Integer = CInt(Int((10 * Rnd()) + 1))
Dim num2 As Integer = CInt(Int((10 * Rnd()) + 1))
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TextBox2.Text = num1 & "*" & num2
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If TextBox1.Text = num1 * num2 Then
Label2.Text = "Correct!!11"
Else
Label2.Text = "Incorrect, sorry about that"
End If
End Sub
End Class
When i run this code, it only generates one question. which is 6*8. If i input 48, it works.But if i click the button again it will not generate another question. It will only generate 6*8. I need it to be able to generate random multiplication questions from 1-10

You're only generating num1 and num2 once, when the instance of the form is initialized, so every time you click the button, it's just reusing the same value.
You should generate a new values every time the button is clicked:
Dim num1 As Integer
Dim num2 As Integer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
num1 = CInt(Int((10 * Rnd()) + 1))
num2 = CInt(Int((10 * Rnd()) + 1))
TextBox2.Text = num1 & "*" & num2
End Sub

If you get 6*8 every time you run the program, including completely stopping and rerunning,then that's really strange and I'm not sure what's up with that.
But if it's just that you're getting the same question every time you click the button, its because you put in a Rnd() when you declared the variables. If you want a new random number each time, you will have to put in some kind of loop where you reassign the variable each time.

Related

.vb keeps giving 0 as total amount you need to pay

So i had to make a program where you can buy stuff when clicking on the buttons, when the amount totals more than 1k you can roll for a discount. This all goes well until I have to use a label to indicate the total amount you need to pay minus the discount. This label keeps staying 0, no matter what. Could anyone help?
Form1 for the "shop":
Public Class Form1
Dim i As Integer = 1
Dim totaalbedrag As Double
Public Sub Actie()
If Val(TextBox3.Text) > 1000 And i = 1 Then
If MsgBox("Wil je strijden voor korting? Zo niet zal deze pop-up niet meer komen dus maak je keuze!", vbYesNo) = vbYes Then
Me.Hide()
Form2.Show()
Else
i = 0
End If
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label6.Text = Val(Label6.Text) + 1
TextBox1.Text = Val(TextBox1.Text) + 250
TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
Actie()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label7.Text = Val(Label7.Text) + 1
TextBox2.Text = Val(TextBox2.Text) + 300
TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
Actie()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
TextBox4.Text = korting & " procent"
totaalbedrag = totaalbedrag - totaalbedrag * korting / 100
TextBox5.Text = Val(totaalbedrag)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
MsgBox("Gefeliciteerd met de aankoop van totaal " & totaalbedrag & " euro!!", vbCritical)
End
End Sub
End Class
Form2 for the gambling:
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
korting = Label1.Text
MsgBox("Gefeliciteerd, je hebt " & korting & " procent korting gekregen!", vbCritical)
Me.Hide()
Form1.Show()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = Int(Rnd() * 10)
End Sub
End Class
Module to have a value as a public one:
Module Module1
Public korting As Integer = 0
End Module
You basically should pay attention to this code of line since I think this is where all goes wrong:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
TextBox4.Text = korting & " procent"
totaalbedrag = totaalbedrag - totaalbedrag * korting / 100
TextBox5.Text = Val(totaalbedrag)
End Sub
I am not at all sure of the timer event in Form1. Every time it ticks the discount is reapplied. If the program runs long enouth totaalbedrag will be very small.
I used .ToString("C") or "c" to display the result as currency. Some of displays are probably not currency. Change to the appropriate format string. https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
For Random numbers please switch to the .net Random class.
Public Class Form1
Dim i As Integer = 1
Dim totaalbedrag As Decimal
Public Sub Actie()
Dim tb3Dec As Decimal
If Decimal.TryParse(TextBox3.Text, tb3Dec) AndAlso tb3Dec > 1000 AndAlso i = 1 Then
If MsgBox("Do you want to compete for a discount? If not, this pop-up will not come again so make your choice!", vbYesNo) = vbYes Then
Me.Hide()
Form2.Show()
Else
i = 0
End If
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
TextBox4.Text = korting & " procent"
totaalbedrag = totaalbedrag - totaalbedrag * korting / 100
TextBox5.Text = totaalbedrag.ToString("C")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim tb1Dec As Decimal
If Not Decimal.TryParse(TextBox1.Text, tb1Dec) Then
MessageBox.Show("Please enter a valid number in TextBox1.")
Exit Sub
End If
Dim tb2Dec As Decimal
If Not Decimal.TryParse(TextBox2.Text, tb2Dec) Then
MessageBox.Show("Please enter a valid number in TextBox2")
Exit Sub
End If
Label6.Text = (CDec(Label6.Text) + 1).ToString
TextBox3.Text = (tb1Dec + tb2Dec).ToString("c")
TextBox1.Text = (tb1Dec + 250).ToString("C")
Actie()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label7.Text = (CDec(Label7.Text) + 1).ToString("c")
Dim tb2Dec As Decimal
If Not Decimal.TryParse(TextBox2.Text, tb2Dec) Then
MessageBox.Show("Please enter a valid number in TextBox2")
Exit Sub
End If
Dim tb1Dec As Decimal
If Not Decimal.TryParse(TextBox1.Text, tb1Dec) Then
MessageBox.Show("Please enter a valid number in TextBox1")
Exit Sub
End If
TextBox2.Text = (tb2Dec + 300).ToString("C")
TextBox3.Text = (tb1Dec + tb2Dec).ToString("C")
Actie()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
MsgBox("Gefeliciteerd met de aankoop van totaal " & totaalbedrag & " euro!!", vbCritical)
End
End Sub
End Class
Public Class Form2
Private rand As New Random
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
korting = CInt(Label1.Text)
MsgBox("Gefeliciteerd, je hebt " & korting & " procent korting gekregen!", vbCritical)
Me.Hide()
Form1.Show()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = rand.Next(0, 101).ToString 'returns an integer 0 to 100
End Sub
End Class
This fixes the Option Strict violations but I have no idea it this works for you. Let us know which label is showing 0.

Sequential/ordinal random number generator

I need it so the next number generated is greater than the last... this is my first project with object-oriented programming, so I don't know much. Also, how do I make it so it runs a certain number of simulations before it lands on a number grouping? It would be greatly appreciated.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
Randomize()
TextBox1.Text = Rand(1, 100)
TextBox2.Text = Rand(1, 100)
TextBox3.Text = Rand(1, 100)
TextBox4.Text = Rand(1, 100)
TextBox5.Text = Rand(1, 100)
TextBox6.Text = Rand(1, 100)
TextBox7.Text = Rand(1, 100)
TextBox8.Text = Rand(1, 200)
End Sub
Public Function Rand(ByVal Low As Long, ByVal High As Long) As Long
Rand = Int((High - Low + 1) * Rnd()) + Low
End Function
End Class
Without getting into any other coding issues with your example:
TextBox2.Text = Rand(Long.Parse(TextBox1.Text), 100)
TextBox3.Text = Rand(Long.Parse(TextBox2.Text), 100)
' ... etc.
The 100 is based off your code, you may have some algorithm for setting the next higher range other than set values. If your first random number is 100 then the rest of your calculations are going to be non-random!
Create a list of the text boxes you want to fill at the form level.
Private lstTextBoxes As List(Of TextBox)
Fill the list in the Form.Load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lstTextBoxes = New List(Of TextBox) From {TextBox1, TextBox2, TextBox3}
End Sub
Use the .net Random class. It is easier than the old one.
Private Rand As New Random
Now you can loop through your text boxes and fill with a "random" number. Each iteration will be a higher number than the last but will stop when it reaches 100.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim highNumber As Integer
For Each txtBox As TextBox In lstTextBoxes
If highNumber >= 99 Then
Return
End If
highNumber = Rand.Next(highNumber + 1, 100)
txtBox.Text = highNumber.ToString
Next
End Sub

I want to display a list of names(3) from a textbox(user input) into a label

This is the code I have so far but is not working properly. The textbox for the names user should input and the button show to display the names in a label in the order entered.
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim intcount, w1, w2, w3 As Integer
Dim intMax As Integer = 2
For intcount = 0 To intMax
strSurnames(intcount) = TextBox1.Text
Next
TextBox1.Clear()
End Sub
Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
Dim intMax As Integer = 2
For intcount = 0 To intMax
lblShow.Text &= strSurnames(intcount)
Next
End Sub
I am guessing you are going to use only 1 textbox and click the "add button" multiple times to store the name. If this is true, you will first need to create
Dim arrayStr As New List(Of String)
Every single time you click on the add button, it will add into this array.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
arrayStr.Add(TextBox1.Text.Trim())
TextBox1.Clear()
End Sub
And to show the full name in just one label when you click on the "Show button", can do it like this
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label1.Text = "" //The label text is cleared.
For i As Integer = 0 To arrayStr.Count() - 1
Label1.Text += arrayStr(i) + ", "
Next
End Sub
UPDATE - BASED ON YOUR COMMENT QUESTION
This is the updated solution based on your question. I am only going to show you with the "Sur Names" only. You can implement the "Weight" the same way.
First, create your array and declare a count as integer for the size of your array.
Dim surNameStr(20) As String
Dim count As Integer = 0
In the "Add Button", you increase the count number by 1 every time you add a new sur name. Once it reached your "maximum" number, you disable your button by BtnAdd.Enabled = False.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If count < 20 Then
surNameStr(count) = TextBox1.Text.Trim()
count = count + 1
Else
Button1.Enabled = False
Button2.Enabled = True
End If
End Sub
Then, in "Show Button", this is how you can show all the surnames stored.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label1.Text = ""
For i As Integer = 0 To 19
Label1.Text += surNameStr(i) + vbNewLine
Next
End Sub

giving calculator input from keyboard keys without pressing calculator buttons

I created a VB.net calculator application.It works fine but say I want to add 5+7.Then as my application works I can press 5 from the keyboard as I gave the text of button with digit 5 as &5 but then if I press + from the keyboard it doesn't work.I have to press the + button in the calculator.
I think that's because my add button is designed to handle click event as btn_add_Click.Is there a way I can make this application to work so that without pressing the buttons in the calculator I can press the keys in keyboard and do the computation.Here's my code:
Imports System.Math
Public Class Form1
Private isFirstExist As Boolean
Private inputOperator As String
Private secondNum As Decimal
Private firstNum As Decimal
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtCalc.TextChanged
End Sub
Private Sub btn_zero_Click(sender As System.Object, e As System.EventArgs) Handles btn_zero.Click
removeFrontZero(0)
End Sub
Private Sub btn_one_Click(sender As System.Object, e As System.EventArgs) Handles btn_one.Click
removeFrontZero(1)
End Sub
Private Sub btn_two_Click(sender As System.Object, e As System.EventArgs) Handles btn_two.Click
removeFrontZero(2)
End Sub
Private Sub btn_clear_Click(sender As System.Object, e As System.EventArgs) Handles btn_clear.Click
txtCalc.Clear()
txtCalc.Text = "0"
End Sub
'Remove zero which is at the start
Public Sub removeFrontZero(ByVal digit As Integer)
If txtCalc.Text = "0" Then
txtCalc.Text = CStr(digit)
Else
txtCalc.Text &= digit
End If
End Sub
Private Sub btn_add_Click(sender As System.Object, e As System.EventArgs) Handles btn_add.Click
inputOperator = "+"
isFirst()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
isFirstExist = False
End Sub
Private Sub btn_equal_Click(sender As System.Object, e As System.EventArgs) Handles btn_equal.Click
If isFirstExist Then
secondNum = CType(txtCalc.Text, Decimal)
End If
'Calculating the result
Dim result As Decimal = calculate(firstNum, secondNum, inputOperator)
txtCalc.Text = result.ToString()
isFirstExist = False
End Sub
Private Function calculate(ByVal num1 As Decimal, ByVal num2 As Decimal, ByVal inputOp As String) As Decimal
Dim output As Decimal
firstNum = num1
secondNum = num2
Select Case inputOp
Case "+"
output = num1 + num2
Case "-"
output = num1 - num2
Case "/"
Dim value As Decimal
Try
isFirst()
value = (num1 / num2)
Catch ex As DivideByZeroException
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK)
End Try
output = value
Case "*"
output = num1 * num2
Case "Mod"
output = (num1 Mod num2)
Case "^"
output = CDec(Math.Pow(num1, num2))
End Select
Return output
End Function
Private Sub isFirst()
If isFirstExist = False Then
firstNum = CType(txtCalc.Text, Decimal)
isFirstExist = True
txtCalc.Text = "0"
End If
End Sub
Assuming you're using the textbox to hold the users input, in the Textchanged or even KeyPressed handler trap the operator keys and run the function instead of displaying the operator.
Something like this:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtCalc.KeyPress
If e.KeyChar = "+"c Then
inputOperator = "+"
isFirst()
e.Handled = True
End If
End Sub
To display the operator set handled to false.

Just generates Answer

Public Class Form1
Dim num1 As Integer = CInt(Int((10 * Rnd()) + 1))
Dim num2 As Integer = CInt(Int((10 * Rnd()) + 1))
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' It is meant to display the question on textbox2.text, not the answer
TextBox2.Text = num1 * num2
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text = num1 * num2 Then
Label2.Text = "Correct!!11"
Else
Label2.Text = "Incorrect, sorry about that"
End If
End Sub
End Class
I want textbox2 to display a question like " 5 * 5" but instead it just displays "25"
You are assigning the value to the textbox as a multiply of num1, and num2. You need to concatenate the string values like this
TextBox2.Text = num1 & " * " & num2