11 Replies - 2285 Views - Last Post: 12 October 2012 - 04:01 AM Rate Topic: -----

#1 LordAizen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-October 12

Rnd in visual basic

Posted 11 October 2012 - 03:52 PM

I need a formula to generate a random number between 1 and 1000 for my class project the one we were given doesn't seem to be working for me which is X = Int(Rnd * (b - a + 1) + a) it doesn't give any errors apart from the fact that rnd becomes rnd() automatically. I've tried to sub in 1000 for b and 1 for a but it still doesn't seem to be working. Thanks for your input people. :whatsthat:

Is This A Good Question/Topic? 0
  • +

Replies To: Rnd in visual basic

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Rnd in visual basic

Posted 11 October 2012 - 04:06 PM

You should using the .net methods System.Random and not the vb6 legacy approach.
Dim rng As New System.Random()

Dim x = rng.Next(1,1001) ' x is a random between 1 .. 1001(exclusive)'


This post has been edited by AdamSpeight2008: 11 October 2012 - 04:07 PM

Was This Post Helpful? 1
  • +
  • -

#3 LordAizen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-October 12

Re: Rnd in visual basic

Posted 11 October 2012 - 04:24 PM

Thank you again for your input in helping me though my program isn't working properly i'm not sure why either.
Public Class Form1
    Dim rng As New System.Random()
    Dim Guess, x As Integer
    Dim inthigh As Integer = 0
    Dim intlow As Integer = 1001


    Private Sub Btn_Guess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Guess.Click
        Txt_Guess.Text = Guess
        If Guess = x Then
            MsgBox("You've Guessed Correctly!")
        Else
            If Guess > inthigh Then
                inthigh = Guess
                MsgBox("You're too high!")
            Else
                If Guess < x Then
                    intlow = Guess
                    MsgBox("You're too low!")
                End If
            End If
        End If
    End Sub

    Private Sub Btn_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_new.Click
        Dim x = rng.Next(0, 1001)
    End Sub
End Class



I wnat it so that if you click a button after you've entered a number between 1 & 1000 inclusive then you get a message saying higher or lower as well as changing variables intlow and inthigh. at the moment you always get the correct guess message.
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Rnd in visual basic

Posted 11 October 2012 - 04:32 PM

TxtGuess.Text = Guess is setting the Text property to whatever Guess is. It seems you have the logic reversed. I'm thinking you want Guess to = what the text property of the textbox is.
Was This Post Helpful? 1
  • +
  • -

#5 LordAizen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-October 12

Re: Rnd in visual basic

Posted 11 October 2012 - 04:35 PM

We have been instructed to randomly generate a number and store it in a variable then the user enters a integer into the text box and then a message box to give feedback on the correctness of the guess. I'm using the random generator that the other user gave me Dim x = rng.Next(0, 1001).
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Rnd in visual basic

Posted 11 October 2012 - 04:40 PM

Learning some debugging skills helps too, to understand the actual execution flow of your code.
Was This Post Helpful? 0
  • +
  • -

#7 LordAizen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-October 12

Re: Rnd in visual basic

Posted 11 October 2012 - 04:42 PM

Yes we've just started and I'm a novice only having some basic skills. My apologies.
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Rnd in visual basic

Posted 11 October 2012 - 04:48 PM

You don't have to apologise, I was just pointing it out as it is extremely rare a school teaches them.

This post has been edited by AdamSpeight2008: 11 October 2012 - 04:48 PM

Was This Post Helpful? 1
  • +
  • -

#9 LordAizen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-October 12

Re: Rnd in visual basic

Posted 11 October 2012 - 05:56 PM

Public Class Form1
    Dim rng As New System.Random()
    Dim Guess As Integer
    Dim x As Integer




    Private Sub Btn_Guess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Guess.Click
        Guess = Txt_Guess.Text
        Lbl_test.Text = x
        If Guess > x Then
            MsgBox("You're too high!")
        Else

            If Guess < x Then
                MsgBox("You're too low!")
            Else

                If Guess = x Then
                    MsgBox("You've Guessed Correctly!")
                Else

                    If Guess = "" Then
                        MsgBox("Please guess a number")

                    End If
                End If
            End If
        End If
    End Sub

    Private Sub Btn_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_new.Click

        Dim x = rng.Next(0, 1001)

    End Sub
End Class


I don't think that the first sub is using the value of x from the second sub and I don't know how to make it so.
Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Rnd in visual basic

Posted 11 October 2012 - 07:40 PM

OK, this is a scope issue. You see at the top where you Dim x as integer? You just defined a variable that can be accessed throughout the form (class).

However...

In your btn_New click event, look at what you did with X

If you Dim a variable in a sub, it is lost when the sub exits.

Since you already Declared x in the scope of the entire class, you only need to set x to a value

You instead have re-declared x to the subroutine for btn_new

Fix the line in btn_New and then test your program.

The only other thing I will point out to help is any time you use
Else
  If ...

You can typically use ElseIf instead.

If x = 1 then 
 'do something for 1
ElseIf x = 2 then
 'so something for 2
Else
 'do something for any other value in x other than 1 or 2
End If

This post has been edited by CharlieMay: 11 October 2012 - 07:42 PM

Was This Post Helpful? 1
  • +
  • -

#11 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Rnd in visual basic

Posted 12 October 2012 - 01:46 AM

About random number generator, I personally think that using Static declaration returns more "real" random results, than when you are declaring it with Dim or any other ever changing declaration keyword. With Static, the sequence of generated pseudo-random values remains the same, so that when you're calling .Next(), it actually uses next value from this sequence. With Dim, if seed integer is the same, it produces the same sequence of pseudo-random values, and the results of such random generator are the same. For more clarification, read about Random Class on MSDN, under Remarks section.

To ilustrate what I'm talking about...

Public Class Form1
    Private g As Graphics

    Private Sub Form1_Load(sender As System.Object,
                           e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        Me.g = Me.CreateGraphics
        Me.RaisePaintEvent(Me, Nothing)
    End Sub

    Private Sub Form1_Paint(sender As Object,
                        e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

        For n = 0 To 100
            Dim colorSelector As Integer = GiveMeRandomNumber(1, 8)
            Dim mPen As New Pen(Color.Black, 1)

            Select Case colorSelector
                Case 1
                    mPen.Color = Color.Red
                Case 2
                    mPen.Color = Color.Black
                Case 3
                    mPen.Color = Color.Firebrick
                Case 4
                    mPen.Color = Color.MistyRose
                Case 5
                    mPen.Color = Color.Gold
                Case 6
                    mPen.Color = Color.Azure
                Case 7
                    mPen.Color = Color.Green
            End Select
            Dim x As Integer = GiveMeRandomNumber(0, 101)
            Dim y As Integer = GiveMeRandomNumber(0, 101)
            Me.g.FillRectangle(mPen.Brush, x * 6, y * 6, 15, 15)
            Me.g.DrawRectangle(Pens.Black, x * 6, y * 6, 15, 15)
        Next
    End Sub

    Public Function GiveMeRandomNumber(ByVal min As Integer,
                                       ByVal max As Integer) As Integer
        Static rng As New System.Random(System.DateTime.Now.Millisecond)
        'Dim rng As New System.Random(System.DateTime.Now.Millisecond)
        Dim x As Integer = rng.Next(min, max)

        Return x
    End Function
End Class



Using Dim to declare Random, and using it for graphical objects' locations produces:

Attached Image

With Static for the same purpose, you get:

Attached Image

I think that image says more than all the words above. You can try the example code, to see it in action yourself.
Was This Post Helpful? 1
  • +
  • -

#12 manos364  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 09-October 12

Re: Rnd in visual basic

Posted 12 October 2012 - 04:01 AM

Simple Function for this.
    Dim objRandom As New System.Random( CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))

    Public Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 10000) As Integer
        ' Returns a random number,
        ' between the optional Low and High parameters
        Return objRandom.Next(Low, High + 1)
    End Function


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1