Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 29th, 2004, 07:32 AM
Authorized User
 
Join Date: Sep 2004
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Brettvan1
Default Visual basic.net (Lottery program)

Hi there,
Could anyone help me with writing the code for six random numbers to appear in six different list boxes, and to have them appear in ascending order.Any help or hints would be greatly appreciated.

Thanks

 
Old September 29th, 2004, 08:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

What do you have so far? We can help you problems, but we aren't going to write it for you. A hint is to look into the Random Class.

J
 
Old September 29th, 2004, 01:59 PM
Authorized User
 
Join Date: Sep 2004
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Brettvan1
Default

Ok here is what I have so far....

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = CStr(Int(Rnd() * 50))
        Label2.Text = CStr(Int(Rnd() * 50))
        Label3.Text = CStr(Int(Rnd() * 50))
        Label4.Text = CStr(Int(Rnd() * 50))
        Label5.Text = CStr(Int(Rnd() * 50))
        Label6.Text = CStr(Int(Rnd() * 50))

    End Sub
This generates random numbers from 0-49 in my six boxes, but sometimes the numbers are the same and I cant seem to get them into ascending order.Can anyone lend a hand here?

Any help would be much appreciated.

 
Old September 29th, 2004, 10:17 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Try something like this:

-------------------------------------------------------------

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim rnd As Random = New Random

        Dim RandomNumber As Integer
        Dim i, b, x(5), counter As Byte
        Dim ctrl As Control

        For i = 1 To 6

            RandomNumber = rnd.Next(1, 54)

            For Each ctrl In Me.Controls
                If TypeOf ctrl Is Label And ctrl.Name = "Label" & i.ToString Then
                    x(i - 1) = RandomNumber
                    counter = 0
                    For b = 0 To 5
                        If x(b) = x(i - 1) Then
                            counter += 1
                        End If
                    Next

                    If counter > 1 Then
                        i -= 1
                    Else
                        ctrl.Text = RandomNumber
                    End If
                End If
            Next
        Next

End Sub

-------------------------------------------------------------

J
 
Old September 30th, 2004, 02:57 AM
Authorized User
 
Join Date: Sep 2004
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Brettvan1
Default

Thanks so much for your help.I am still a little baffled though, (sorry still very new to all of this)Are you saying that the bit I already have is ok and I now need to add this? (is this the simplest way, do you think?)

Once again thanks again for taking the time out to help me.

Kind regards
Brett

 
Old September 30th, 2004, 08:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Replace what you have with this.

J
 
Old September 30th, 2004, 09:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

By the way, I just saw your other post. I assume that these numbers are what you are trying to sort.

The code I gave you will randomly select a number between 1 and 53. Replace:

RandomNumber = rnd.Next(1, 54)

With:

RandomNumber = rnd.Next(0, 50)

I believe, and someone can correct me if I am wrong, that the upperbound number is one greater than the highest number you want returned but the lower bound needs to be set to the lowest number you want returned.

J
 
Old September 30th, 2004, 09:16 AM
Authorized User
 
Join Date: Sep 2004
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Brettvan1
Default

Thanks for your help, will give it a crack tonight after work and let you know how it pans out.

B

 
Old October 1st, 2004, 03:52 AM
Authorized User
 
Join Date: Sep 2004
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Brettvan1
Default

Ok so thanks to your help I have come up with this code so far...
This is giving me random numbers, but now I cant seem to get them into ascending order.Could anyone help me with this?

dim intarray(49) as integer
dim intcount as integer
dim intindex as integer
dim repeated as boolean
dim obj as object

label1.text=intarray(0)
label2.text=intarray(1)
label3.text=intarray(2)
label4,text=intarray(4)
label5.text=intarray(5)
label6.text=intarray(6)

For intcount=0 to 5
Do
repeated=false
intnumber=int((50 * rnd())+1)

for intindex=0 to 5
if intnumber=intarray(intindex) then
repeated=true
end if
Next
Loop until repeated=False

intarray(intcount)=intnumber

Next
intindex= -1
for each obj in groupbox1.controls
intindex+=1
obj.text=intarray(intindex)

Next
end if

 
Old October 1st, 2004, 08:19 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Brett,

Have you tried what I proposed. Rnd is a VB6 keyword. You should use the Random class as I showed you.

If you are placing them in a group box, you will need to replace:

Me.Controls

with your groupbox name like:

[GroupBoxName].Controls

I assume this is groupbox1, so replace it with that.

Also, since you have an array - "x(5)", you can call the sort method of the array with on line of code to get the numbers in ascending order. Then you can loop through the controls and set the text property.

--------------------------------
        'sort the array
        Array.Sort(x)
        'reset the counter
        i = 1
        For i = 1 To 6
            For Each ctrl In groupbox1.Controls

                'check the controls to see if they are a label and named label(i)
                If TypeOf ctrl Is Label And ctrl.Name = "Label" & i.ToString Then
                    ctrl.Text = x(i - 1)
                    Exit For
                End If
            Next
        Next

--------------------------------

J





Similar Threads
Thread Thread Starter Forum Replies Last Post
visual basic.net pringlesoft VB.NET 2 February 11th, 2008 01:46 PM
Lottery Program Help...I'm using 2005 Visual Basic psycoblaster VB.NET 2002/2003 Basics 13 May 23rd, 2007 09:13 PM
Lottery Winners Program Urgent Help Needed! Honour C++ Programming 2 May 19th, 2007 04:47 AM
Rowfilter in Visual Basic.Net ahooi ASP.NET 1.0 and 1.1 Basics 0 July 5th, 2005 03:51 AM
writing a lottery program in C++ captlem66 VB.NET 2002/2003 Basics 2 October 13th, 2004 02:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.