Random number

steveb46

Member
Joined
Aug 5, 2010
Messages
15
Programming Experience
Beginner
I have a problem. I'm trying to generate a random number 'x' amount of times. I get a random number but I'm getting the same number repeated over and over. I need a random number each time. What am I doing wrong?

VB.NET:
    Private Function RandomNumber(ByVal min As Integer, ByVal max As Integer) As Integer
        Dim random As New Random()
        Return random.Next(min, max)
    End Function 'RandomNumber 



    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim objFSO
        objFSO = Server.CreateObject("Scripting.FileSystemObject")

        Dim objTextStream
        Dim strFileName = System.AppDomain.CurrentDomain.BaseDirectory() & "chartdata.xml"

        'set constant for writing
        Const fsoForWriting = 2
        objTextStream = objFSO.OpenTextFile(strFileName, fsoForWriting)
        Dim x
        Dim myvalue As Integer = 0
        objTextStream.WriteLine("<?xml version=" & Chr(34) & "1.0" & Chr(34) & "?>" & vbCrLf)
        objTextStream.WriteLine("<chart>" & vbCrLf)
        Dim ran As New Random
        Random.Next(0, 100)

        For x = 1 To Int(TextBox5.Text)
            myvalue = 0
            myvalue = RandomNumber(0, 1000)
            objTextStream.WriteLine("<chartdata>")
            objTextStream.WriteLine("<YEAR>" & x & "</YEAR>")
            objTextStream.writeline("<" & Replace(TextBox1.Text, " ", "_") & ">" & myvalue & "</" & Replace(TextBox1.Text, " ", "_") & ">")
            objTextStream.WriteLine("</chartdata>" & vbCrLf)
        Next


        objTextStream.WriteLine("</chart>" & vbCrLf)

        'Close the file and clean up
        objTextStream.Close()
        objTextStream = Nothing
        objFSO = Nothing
        Response.Redirect("~/default5.aspx")
    End Sub
End Class
 
Keep a single instance of Random class and use that to generate all random numbers.
 
Duh, it was a long day. Not calling it from a function, but using

Dim ran As New Random

myvalue = ran.Next(0, 1000)

in-line worked. Thanks!
 
Pulling my hair out...

Random is not working properly. I tried this in a windows form just to see if it works, it does.

Here's my code:

VB.NET:
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim objFSO
        objFSO = Server.CreateObject("Scripting.FileSystemObject")

        Dim objTextStream
        Dim strFileName = System.AppDomain.CurrentDomain.BaseDirectory() & "chartdata.xml"

        'set constant for writing
        Const fsoForWriting = 2
        objTextStream = objFSO.OpenTextFile(strFileName, fsoForWriting)
        Dim x
        Dim myvalue1 As Integer
        Dim myvalue2 As Integer
        Dim myvalue3 As Integer
        Dim myvalue4 As Integer
        Dim myvalue5 As Integer
        Dim myvalue1d As Integer
        Dim myvalue2d As Integer
        Dim myvalue3d As Integer
        Dim myvalue4d As Integer
        Dim myvalue5d As Integer
        objTextStream.WriteLine("<?xml version=" & Chr(34) & "1.0" & Chr(34) & "?>" & vbCrLf)
        objTextStream.WriteLine("<chart>" & vbCrLf)
        Dim myseries1 = "", myseries2 = "", myseries3 = "", myseries4 = "", myseries5 = ""
        Dim seriesno As Integer


        seriesno = 1
        If TextBox1.Text = "" Then
            myseries1 = "Series1"
        Else
            myseries1 = TextBox1.Text
        End If

        If Int(DropDownList3.SelectedValue) > 1 Then
            seriesno = 2
            If TextBox12.Text = "" Then
                myseries2 = "Series2"
            Else
                myseries2 = TextBox12.Text
            End If
        End If

        If Int(DropDownList3.SelectedValue) > 2 Then
            seriesno = 3
            If TextBox13.Text = "" Then
                myseries3 = "Series3"
            Else
                myseries3 = TextBox13.Text
            End If
        End If
        If Int(DropDownList3.SelectedValue) > 3 Then
            seriesno = 4
            If TextBox14.Text = "" Then
                myseries4 = "Series4"
            Else
                myseries4 = TextBox14.Text
            End If
        End If
        If Int(DropDownList3.SelectedValue) > 4 Then
            seriesno = 5
            If TextBox15.Text = "" Then
                myseries5 = "Series5"
            Else
                myseries5 = TextBox15.Text
            End If
        End If
        Dim temp
        Dim t As Integer = 0
        Dim ran As New Random
        Dim min As Integer
        Dim max As Integer

        min = Int(txtMin.Text)
        max = Int(txtMax.Text)

        For x = 1 To Int(TextBox5.Text)
            myvalue1 = 0
            myvalue2 = 0
            myvalue3 = 0
            myvalue4 = 0
            myvalue5 = 0
            myvalue1d = 0
            myvalue2d = 0
            myvalue3d = 0
            myvalue4d = 0
            myvalue5d = 0
            '            myvalue = RandomNumber(0, 1000)


            myvalue1 = ran.Next(min, max)
            myvalue2 = ran.Next(min, max)
            myvalue3 = ran.Next(min, max)
            myvalue4 = ran.Next(min, max)
            myvalue5 = ran.Next(min, max)



            If Int(TextBox4.Text) > 0 Then
                'handle 1st series decimal
                myvalue1d = ran.Next(0, Int(10 ^ TextBox4.Text) - 1)
                If Len(myvalue1d) < Int(TextBox4.Text) Then
                    temp = Int(TextBox4.Text) - Len(myvalue1d)
                    myvalue1d = CType(myvalue1d, String)

                    For t = 1 To Int(temp)
                        myvalue1d = "0" & myvalue1d
                    Next
                End If
                myvalue1d = "." & myvalue1d

                Response.Write(myvalue1d & "," & txtMin.Text & "," & txtMax.Text)

                'handle 2nd series decimal
                myvalue2d = ran.Next(0, Int(10 ^ TextBox4.Text) - 1)
                If Len(myvalue2d) < Int(TextBox4.Text) Then
                    temp = Int(TextBox4.Text) - Len(myvalue2d)
                    myvalue2d = CType(myvalue2d, String)
                    For t = 1 To Int(temp)
                        myvalue2d = "0" & myvalue2d
                    Next
                End If
                myvalue2d = "." & myvalue2d

                'handle 3rd series decimal
                myvalue3d = ran.Next(0, Int(10 ^ TextBox4.Text) - 1)
                If Len(myvalue3d) < Int(TextBox4.Text) Then
                    temp = Int(TextBox4.Text) - Len(myvalue3d)
                    myvalue3d = CType(myvalue3d, String)
                    For t = 1 To Int(temp)
                        myvalue3d = "0" & myvalue3d
                    Next
                End If
                myvalue3d = "." & myvalue3d

                'handle 4th series decimal
                myvalue4d = ran.Next(0, Int(10 ^ TextBox4.Text) - 1)
                If Len(myvalue4d) < Int(TextBox4.Text) Then
                    temp = Int(TextBox4.Text) - Len(myvalue4d)
                    myvalue4d = CType(myvalue4d, String)
                    For t = 1 To Int(temp)
                        myvalue4d = "0" & myvalue4d
                    Next
                End If
                myvalue4d = "." & myvalue4d

                'handle 5th series decimal
                myvalue5d = ran.Next(0, Int(10 ^ TextBox4.Text) - 1)
                If Len(myvalue5d) < Int(TextBox4.Text) Then
                    temp = Int(TextBox4.Text) - Len(myvalue5d)
                    myvalue5d = CType(myvalue5d, String)
                    For t = 1 To Int(temp)
                        myvalue5d = "0" & myvalue5d
                    Next
                End If
                myvalue5d = "." & myvalue5d
            End If


            objTextStream.WriteLine("<chartdata>")
            objTextStream.WriteLine("<YEAR>" & x & "</YEAR>")
            objTextStream.writeline("<" & Replace(myseries1, " ", "_") & ">" & myvalue1 & myvalue1d & "</" & Replace(myseries1, " ", "_") & ">")
            If seriesno > 1 Then
                objTextStream.writeline("<" & Replace(myseries2, " ", "_") & ">" & myvalue2 & myvalue2d & "</" & Replace(myseries2, " ", "_") & ">")
            End If
            If seriesno > 2 Then
                objTextStream.writeline("<" & Replace(myseries3, " ", "_") & ">" & myvalue3 & myvalue3d & "</" & Replace(myseries3, " ", "_") & ">")
            End If
            If seriesno > 3 Then
                objTextStream.writeline("<" & Replace(myseries4, " ", "_") & ">" & myvalue4 & myvalue4d & "</" & Replace(myseries4, " ", "_") & ">")
            End If
            If seriesno > 4 Then
                objTextStream.writeline("<" & Replace(myseries5, " ", "_") & ">" & myvalue5 & myvalue5d & "</" & Replace(myseries5, " ", "_") & ">")
            End If
            objTextStream.WriteLine("</chartdata>" & vbCrLf)
        Next


        objTextStream.WriteLine("</chart>" & vbCrLf)

        'Close the file and clean up
        objTextStream.Close()
        objTextStream = Nothing
        objFSO = Nothing
        Response.Redirect("~/default4.aspx")
    End Sub
End Class

Min is coming from txtMin (textbox). Max is coming from txtMin (textbox). If I run the application, change min to -20 and max to 80, this is the result. The purpose of what I'm doing here is for MSChart control. Trying to create data on the fly. My decimal rounding isn't working either. The user could enter a decimal so that it randomly generates a number, and then randomly generates decimals.

Here's the resultant XML that I'm writing:

HTML:
<?xml version="1.0"?>

<chart>

<chartdata>
<YEAR>1</YEAR>
<Series1>160</Series1>
</chartdata>

<chartdata>
<YEAR>2</YEAR>
<Series1>-151</Series1>
</chartdata>

<chartdata>
<YEAR>3</YEAR>
<Series1>-10</Series1>
</chartdata>

<chartdata>
<YEAR>4</YEAR>
<Series1>-120</Series1>
</chartdata>

<chartdata>
<YEAR>5</YEAR>
<Series1>30</Series1>
</chartdata>

<chartdata>
<YEAR>6</YEAR>
<Series1>71</Series1>
</chartdata>

<chartdata>
<YEAR>7</YEAR>
<Series1>-200</Series1>
</chartdata>

<chartdata>
<YEAR>8</YEAR>
<Series1>-81</Series1>
</chartdata>

<chartdata>
<YEAR>9</YEAR>
<Series1>240</Series1>
</chartdata>

<chartdata>
<YEAR>10</YEAR>
<Series1>391</Series1>
</chartdata>

<chartdata>
<YEAR>11</YEAR>
<Series1>-10</Series1>
</chartdata>

<chartdata>
<YEAR>12</YEAR>
<Series1>751</Series1>
</chartdata>

<chartdata>
<YEAR>13</YEAR>
<Series1>-181</Series1>
</chartdata>

<chartdata>
<YEAR>14</YEAR>
<Series1>270</Series1>
</chartdata>

<chartdata>
<YEAR>15</YEAR>
<Series1>190</Series1>
</chartdata>

</chart>


why am I getting numbers greater than 80 and numbers less than 20. Even if I use 0 as min, numbers are greater than. My decimal rounding isn't working at all... (not showing up)
 
Last edited by a moderator:
nevermind. I figured it out. It was a problem with:

If Int(TextBox4.Text) > 0 Then
'handle 1st series decimal
myvalue1d = ran.Next(0, Int(10 ^ TextBox4.Text) - 1)
If Len(myvalue1d) < Int(TextBox4.Text) Then
temp = Int(TextBox4.Text) - Len(myvalue1d)
myvalue1d = CType(myvalue1d, String)

For t = 1 To Int(temp)
myvalue1d = "0" & myvalue1d
Next
End If
myvalue1d = "." & myvalue1d

Response.Write(myvalue1d & "," & txtMin.Text & "," & txtMax.Text)

'handle 2nd series decimal
myvalue2d = ran.Next(0, Int(10 ^ TextBox4.Text) - 1)
If Len(myvalue2d) < Int(TextBox4.Text) Then
temp = Int(TextBox4.Text) - Len(myvalue2d)
myvalue2d = CType(myvalue2d, String)
For t = 1 To Int(temp)
myvalue2d = "0" & myvalue2d
Next
End If
myvalue2d = "." & myvalue2d

'handle 3rd series decimal
myvalue3d = ran.Next(0, Int(10 ^ TextBox4.Text) - 1)
If Len(myvalue3d) < Int(TextBox4.Text) Then
temp = Int(TextBox4.Text) - Len(myvalue3d)
myvalue3d = CType(myvalue3d, String)
For t = 1 To Int(temp)
myvalue3d = "0" & myvalue3d
Next
End If
myvalue3d = "." & myvalue3d

'handle 4th series decimal
myvalue4d = ran.Next(0, Int(10 ^ TextBox4.Text) - 1)
If Len(myvalue4d) < Int(TextBox4.Text) Then
temp = Int(TextBox4.Text) - Len(myvalue4d)
myvalue4d = CType(myvalue4d, String)
For t = 1 To Int(temp)
myvalue4d = "0" & myvalue4d
Next
End If
myvalue4d = "." & myvalue4d

'handle 5th series decimal
myvalue5d = ran.Next(0, Int(10 ^ TextBox4.Text) - 1)
If Len(myvalue5d) < Int(TextBox4.Text) Then
temp = Int(TextBox4.Text) - Len(myvalue5d)
myvalue5d = CType(myvalue5d, String)
For t = 1 To Int(temp)
myvalue5d = "0" & myvalue5d
Next
End If
myvalue5d = "." & myvalue5d
End If

All is well
 
Back
Top