Results 1 to 4 of 4

Thread: [RESOLVED] Random Boolean and Procedure Matching

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Resolved [RESOLVED] Random Boolean and Procedure Matching

    After watching iRobots, and against my better judgment I decided to give my program a brain to play against me.

    In a player's turn, the player can choose to execute 10 different actions (each its own procedure).
    For each computer player, randomize True Or False 10 times, if True, call corresponding procedure, else generate next random. So it decides for itself to carry out that action or not.

    ie

    for x = 1 to 10
    generate true or false
    if true then
    call whatever the first procedure is
    call the 2nd procedure if x = 2
    and so on to 10
    Else
    'do nothing
    End If
    Next

    Any ideas?

    Maybe I just random the strings True and False?
    I would probably need to make some sort of a list to match the value of x to my procedure names.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Random Boolean and Procedure Matching

    Here's how I'd do it:
    Code:
    ''' <summary>
    ''' Random number generator
    ''' </summary>
    Private rng As New Random
    
    ''' <summary>
    ''' References to each of the action methods.
    ''' </summary>
    Private methods As Action() = {New Action(AddressOf Method01),
                                   New Action(AddressOf Method02),
                                   New Action(AddressOf Method03),
                                   New Action(AddressOf Method04),
                                   New Action(AddressOf Method05),
                                   New Action(AddressOf Method06),
                                   New Action(AddressOf Method07),
                                   New Action(AddressOf Method08),
                                   New Action(AddressOf Method09),
                                   New Action(AddressOf Method10)}
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each method In methods
            'Generate a random number and, if it's even, execute the next method.
            If rng.Next() Mod 2 = 0 Then
                method.Invoke()
            End If
        Next
    End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: Random Boolean and Procedure Matching

    jmcilhinney, you are not just a coder, you are an artist.
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: Random Boolean and Procedure Matching

    I wasn't sure this worked at first when I tested it to learn. On first click I drew 2,4,6,8 and then on the second click I drew 3,5,7,9. After further clicking it was more random and it does exactly what it is supposed to do.

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width