Jump to content

Recommended Posts

Posted

My first "help" thread here :)

I am wrting a Visual Basic application that will need to use an array with anywhere from 2 to 10 elements that contain unique numbers within a range generated randomally. I'm using VS 2008 for this. The code I got for this in a test project I'm doing until I can figure out how its done:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Array to be used

Dim randomnumbers(9) As Integer

'String that well contain the values to display in the messagebox

Dim messagestring As String = ""

'Cycles through each element in the array

For I As Integer = 0 To randomnumbers.Count - 1

'Assigns the current I element a random number between the range of 1 and 10

randomnumbers(I) = CInt(Int((10 * Rnd()) + 1))

'Only happens on the 2nd or any other go-around since there well never be a first answer that is the same when the others have not be given values.

If I > 0 Then

'Compares the current element to the last one. If equal, decrease I so the same element is used in the next go around.

If randomnumbers(I) = randomnumbers((I - 1)) Then

I -= 1

Else

'Appends the current element to the string to be displayed at the end when it is unique from previous elements

messagestring &= randomnumbers(I) & " "

End If

Else

'Assigns the first element to the string to be displayed at the end of the procedure.

messagestring &= randomnumbers(I) & " "

End If

Next

MessageBox.Show(messagestring)

End Sub

End Class

The code above does generate numbers between the ranges of 1-10 for the ten elements of the array, but despite the if condition I put in to redo the element if it equals the last element, it ends up giving me two or more elements with the same number just about everytime I execute the code.

Posted

i have a similar program which i know to work by generating random numbers and assigning them to picture boxex for a guessing game. here is the code:

' Project name: Mouse Game Project

' Project purpose: Simulates the Find The Mouse game.

' Created/revised by: Cole ***** on 1/13/09

Option Explicit On

Option Strict On

Option Infer On

Public Class MainForm

' form-level variable for storing a random number

Private randomNumber As Integer

Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click

Me.Close()

End Sub

Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click

'prepares the interface for a new game

Dim ranGen As New Random

'generate random number from 1 through 5

randomNumber = ranGen.Next(1, 6)

'display the "Is the mouse here?" image

PictureBox1.Image = questionPictureBox.Image

PictureBox2.Image = questionPictureBox.Image

PictureBox3.Image = questionPictureBox.Image

PictureBox4.Image = questionPictureBox.Image

PictureBox5.Image = questionPictureBox.Image

End Sub

Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

'displays either the mouse image or the not here image

If randomNumber = 1 Then

PictureBox1.Image = mousePictureBox.Image

Else

PictureBox1.Image = notHerePictureBox.Image

End If

End Sub

Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click

If randomNumber = 2 Then

PictureBox2.Image = mousePictureBox.Image

Else

PictureBox2.Image = notHerePictureBox.Image

End If

End Sub

Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click

If randomNumber = 3 Then

PictureBox3.Image = mousePictureBox.Image

Else

PictureBox3.Image = notHerePictureBox.Image

End If

End Sub

Private Sub PictureBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox4.Click

If randomNumber = 4 Then

PictureBox4.Image = mousePictureBox.Image

Else

PictureBox4.Image = notHerePictureBox.Image

End If

End Sub

Private Sub PictureBox5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox5.Click

If randomNumber = 5 Then

PictureBox5.Image = mousePictureBox.Image

Else

PictureBox5.Image = notHerePictureBox.Image

End If

End Sub

End Class

hope this helps!! :)

Posted

Hmm.. using the random class to do it. I tried that a few days ago and kept getting the same number, but the .next(1, 6) might work. Don't have time now to fiddle with it but I'll give it a try. Any more suggestions to try out? BTW, this isn't a game. Its a quiz app that needs to randomally display questions and thier possible answers hence the range restrictions and importance that each be unique.

Posted

Well, I was hopeful but it ended up producing the same result with one or more elements having the same value...

I orginally had another loop set up to catch for things like this, but I was forced to go to the code I got above because it kept returning the same value the way I had it, causing it to loop continously with no logical nor runtime errors.

Posted

Well I came up with a solution on my own... after all this trouble :) Here's the code if anyone's interested:

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim str As String = "" 'String of numbers in thier random order to show
        Dim RandomNumbers(0) As Decimal 'Array of random numbers used to sort the real numbers randomally so they are unique
        Dim Numbers(9) As Integer 'Array of actual numbers the application will use
        Numbers(0) = 1
        Numbers(1) = 2
        Numbers(2) = 3
        Numbers(3) = 4
        Numbers(4) = 5
        Numbers(5) = 6
        Numbers(6) = 7
        Numbers(7) = 8
        Numbers(8) = 9
        Numbers(9) = 10
        For I As Integer = 0 To Numbers.Count - 1
            ReDim Preserve RandomNumbers(I)
            RandomNumbers(I) = Rnd()
        Next
        Array.Sort(RandomNumbers, Numbers)
        For I As Integer = 0 To Numbers.Count - 1
            str &= Numbers(I) & " "
        Next
        MessageBox.Show(str)
    End Sub
End Class

Couple more routines to write and I'll be done with my first useful application :)

  • 10 months later...
Posted

Just wanted to mention that it's so nice to finally see someone that uses VB.NET. Everybody I know writes in C or C++, I never see anyone using VB.NET. It happens to be my main area so it's sort of depressing.

It's also interesting to see another quiz program. I made one a few months back for my girlfriend for one of her classes in school. It was fun :P.

Anyways, keep working with VB.NET! Despite what others say, it's quite a capable language. I have yet to find something I can't do with it :D. (Here comes the torrential downpour of complaints about it :P)

  • 2 weeks later...
Posted

Well I use VB cause thats the language I'm most comfortable with and was taught in school. I've tried C# in the past which is practically the same but anytime I have problems I can just about instantly think of the right way to do it in VB :)

My only complaint is the .net dependency, but times are changing and .net is included in Windows releases by default now.

Posted
add :

Randomize()

yeah really this simplify everything unless you have to use an array for an assignment, like I have to do sometimes in class. But if not, that is defiantly the way to go, just output it in a label and you're good

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...