Random text generator

Hi,

I'm very new to VB Express, I'm taking a college class about it. For a project, I would like to display a set of random statements when the user clicks a button, like a fortune cookie program. So when the user clicks btnSaying, I want one of eight statements to display. It is probably far more simple than I think, but for some reason I can't figure out how to set this up. I've read all kinds of different posts about random numbers and letters and arrays and things like that. I think I might just be lost. Please help me.

[546 byte] By [SamB42083] at [2007-12-24]
# 1

sure.

Basically you need a way to store your "statements", for simple reasons lets stick with an ArrayList.

Dim theStatements as new ArrayList()

declare that at a global level so other methods/functions in your class can access it.

Next up, at some stage you will be populating/filling the array with your statements. To do say, say if we had a button called "Fill statements", then in this double click event we would do something like this:

Me.theStatements.Clear()

Me.theStatements.Add("Statement 1")

Me.theStatements.Add("Statement 2")

..

..

Next up, the final part, when the user click on the main button to show your random statement, simply do this on the double click event of that button (Double click it on the designer view to create the event)

Dim theRandomNumber as new Random()

Dim theNumberChosen as Integer = theRandomNumber.Next(0, Me.theStatements.Count)

MessageBox.Show(Me.theStatements(theNumberChosen).ToString())

this will:

  • create a random class instance

  • get a random number between 0 and the number of items in the arraylist

  • show you the message, at the specified location int the array.

    does this help?

  • ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
    # 2

    Actually you did not provide a seed so the random class will produce the same sequence of
    of numbers.

    seeding can be produced thusly:

    Dim r As New Random(Now.Ticks And &HFFFFFFF)

    Dim num As Short = r.Next(0, 8)

    ReneeC at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
    # 3
    Thank you both SO much! It took some working but I got it going. I couldn't have done it without your help.
    SamB42083 at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
    # 4

    How can you make a Array global?

    I am new to VB so please Help me. thanks in advance

    BobRistocrof at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
    # 5

    simply declare it at the top of the class. Example:

    public class MyClass

    private myArray as new ArrayList()

    ..

    ..

    private sub MyFunction()

    Me.myArray.Add("hi!") 'adds item into the global array variable

    end sub

    end class

    ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
    # 6

    Amhedilyas's nomenclature was incorrect. The correct name is "member variable" not global. They are not the same thing.

    ReneeC at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
    # 7
    What is the purpose of AND'ing the Now.Ticks with &HFFFFFFF? I generally use Environment.TickCount:

    Dim r As New Random(Environment.TickCount)

    Cheers

    ChrisDunaway at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...