Random Numbers Generator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dr3amxstar
    New Member
    • May 2007
    • 55

    Random Numbers Generator

    Hi All, sorry for posting so many questions. Im a newbie but im trying to learn :D
    With Regards to SammyB Code :

    Code:
    Public Class Form1
        Private oRand As Random
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            oRand = New Random(DateTime.Now.Millisecond)
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim iRand As Integer
            iRand = oRand.Next(1, 10)
            MsgBox(iRand)
        End Sub
    End Class
    I tried implementing it into my program but i change this line
    iRand = oRand.Next(1,10 ) to iRand = oRand.Next(-0.3,0.3) as i want my range to be from -0.3 to 0.3. However the the value 0 kept appearing when i click the button. Why is it so? I will have to store the random values in an array as well. array(0 To 10919). Is it possible as well?
  • ChillUmesh
    New Member
    • Feb 2007
    • 20

    #2
    Originally posted by dr3amxstar
    Hi All, sorry for posting so many questions. Im a newbie but im trying to learn :D
    With Regards to SammyB Code :

    Code:
    Public Class Form1
        Private oRand As Random
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            oRand = New Random(DateTime.Now.Millisecond)
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim iRand As Integer
            iRand = oRand.Next(1, 10)
            MsgBox(iRand)
        End Sub
    End Class
    I tried implementing it into my program but i change this line
    iRand = oRand.Next(1,10 ) to iRand = oRand.Next(-0.3,0.3) as i want my range to be from -0.3 to 0.3. However the the value 0 kept appearing when i click the button. Why is it so? I will have to store the random values in an array as well. array(0 To 10919). Is it possible as well?

    Try this one
    for i=0 to 10919
    array(i)=(rnd*0 .6)-0.3
    next


    this generates random numbers ranging from -0.3 to 0.3

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      The problem is that you are storing these numbers in an Integer variable, which can only hold whole numbers. They are rounded to the nearest integer, which in this case is zero. Try using another numeric data type such as Single.

      Comment

      • dr3amxstar
        New Member
        • May 2007
        • 55

        #4
        Originally posted by Killer42
        The problem is that you are storing these numbers in an Integer variable, which can only hold whole numbers. They are rounded to the nearest integer, which in this case is zero. Try using another numeric data type such as Single.
        Previously i have tried changing it the double and single as well but it doesnt work also.. It gave me zero too

        Comment

        • dr3amxstar
          New Member
          • May 2007
          • 55

          #5
          Originally posted by ChillUmesh
          Try this one
          for i=0 to 10919
          array(i)=(rnd*0 .6)-0.3
          next


          this generates random numbers ranging from -0.3 to 0.3
          Hi i tried and it works. I just want to ask, how is it different from sammyB code? why a seed is not used in here or it is not neccessary at all? Why do you times 0.6?

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by dr3amxstar
            Hi i tried and it works. I just want to ask, how is it different from sammyB code? why a seed is not used in here or it is not neccessary at all? Why do you times 0.6?
            This code is simply generating a random number between zero and 0.6 (twice your limit) then subtracting half. The effect of this is to give you a number between -0.3 and +0.3.

            Also, array() must be of a different type to the integer variable you were using before. If you didn't specify the type, then I suppose it will be the default type, which would be Variant in VB6, and I think Object in later versions.

            Comment

            • dr3amxstar
              New Member
              • May 2007
              • 55

              #7
              Originally posted by Killer42
              This code is simply generating a random number between zero and 0.6 (twice your limit) then subrtacting half. The effect of this is to give you a number between -0.3 and +0.3.

              Also, array() must be of a different type to the integer variable you were using before. If you didn't specify the type, then I suppose it will be the default type, which would be Variant in VB6, and I think Object in later versions.
              Hmm this simple code suits my program since i only need to generate random code once throughout my entire program. thanks alot for all your help! Im on the right track now i hope! :D

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by dr3amxstar
                Hmm this simple code suits my program since i only need to generate random code once throughout my entire program. thanks alot for all your help! Im on the right track now i hope! :D
                Glad we could help. :)

                Let us know how it turns out.

                (If you come up with new questions along the way, please post them as new discussion threads, don't just tack them onto the end of this one. People often ignore old threads, and just look for new ones to answer, so you'll get a better response that way.)

                Comment

                • dr3amxstar
                  New Member
                  • May 2007
                  • 55

                  #9
                  Originally posted by Killer42
                  Glad we could help. :)

                  Let us know how it turns out.

                  (If you come up with new questions along the way, please post them as new discussion threads, don't just tack them onto the end of this one. People often ignore old threads, and just look for new ones to answer, so you'll get a better response that way.)
                  Ok i got it! I will post my code as soon as i am done with it! :D

                  Comment

                  • SammyB
                    Recognized Expert Contributor
                    • Mar 2007
                    • 807

                    #10
                    Originally posted by dr3amxstar
                    Hi All, sorry for posting so many questions. Im a newbie but im trying to learn :D
                    With Regards to SammyB Code, http://www.thescripts.com/forum/thread641768.html

                    I tried implementing it into my program but i change this line
                    iRand = oRand.Next(1,10 ) to iRand = oRand.Next(-0.3,0.3) as i want my range to be from -0.3 to 0.3. However the the value 0 kept appearing when i click the button. Why is it so? I will have to store the random values in an array as well. array(0 To 10919). Is it possible as well?
                    Glad that you figured it out. I tried to post last night, but the router crashed. Assuming that you are using VB.Net, as you discovered oRand.Next(...) always returns an integer; however, there is also .NextDouble() which is the same as VB6 rnd(). So, the code should look like:
                    Code:
                    Public Class Form1
                    	Private oRand As Random
                    	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                    		oRand = New Random(DateTime.Now.Millisecond)
                    	End Sub
                    	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                    		Dim dRand As Double
                    		dRand = oRand.NextDouble()
                    		Dim dOffset As Double = -0.3
                    		Dim dRange As Double = 0.3 - dOffset	' = 0.6 in this case
                    		dRand = oRand.NextDouble() * dRange + dOffset
                    		MessageBox.Show(dRand)
                    	End Sub
                    End Class

                    Comment

                    • dr3amxstar
                      New Member
                      • May 2007
                      • 55

                      #11
                      Originally posted by SammyB
                      Glad that you figured it out. I tried to post last night, but the router crashed. Assuming that you are using VB.Net, as you discovered oRand.Next(...) always returns an integer; however, there is also .NextDouble() which is the same as VB6 rnd(). So, the code should look like:
                      Code:
                      Public Class Form1
                      	Private oRand As Random
                      	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                      		oRand = New Random(DateTime.Now.Millisecond)
                      	End Sub
                      	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                      		Dim dRand As Double
                      		dRand = oRand.NextDouble()
                      		Dim dOffset As Double = -0.3
                      		Dim dRange As Double = 0.3 - dOffset	' = 0.6 in this case
                      		dRand = oRand.NextDouble() * dRange + dOffset
                      		MessageBox.Show(dRand)
                      	End Sub
                      End Class
                      Thanks! I tried and it works too. May i know what is the difference between this code and the code from ChillUmesh?

                      Comment

                      • bIGMOS
                        New Member
                        • Dec 2007
                        • 5

                        #12
                        Originally posted by dr3amxstar
                        Thanks! I tried and it works too. May i know what is the difference between this code and the code from ChillUmesh?
                        Hi. I am trying to generate random numbers between 1000 and 9999.

                        [CODE=vbnet]Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
                        Randomize()
                        Dim iRand As Integer
                        iRand = oRand.Next(1000 , 9999)

                        ' This is where I want 4 DIFFRENT RANDOM NUMBERS BUT THEY'RE RANDOMLY THE SAME AHHH''''
                        'TextBox1.Text = (iRand)
                        'TextBox2.Text = (iRand)
                        'TextBox3.Text = (iRand)
                        'TextBox4.Text = (iRand)
                        End Sub[/CODE]
                        How can I get 4 different random numbers at each click? Thanks in advance for the help.
                        Last edited by Killer42; Dec 19 '07, 08:32 PM. Reason: Added CODE=vbnet tag

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          Look at it logically. You generated a random number, and placed it in a variable. However many times you access that variable it will still contain the same value, unless you change it.

                          I'd say the most obvious options are...
                          • Assign another random number to variable iRand before each of the statements where you use it.
                          • Create an array with four elements, and place a random number in each, then use those. (This is probably overkill, though.)
                          • Don't bother placing the number in a variable at all - just use the oRand.Next(1000 , 9999) in each case.

                          Comment

                          Working...