dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: SOS:How to simulati the random event with a coin?

  1. #1
    Join Date
    Sep 2004
    Posts
    96

    SOS:How to simulati the random event with a coin?

    Hi,I want to make a small program with simulating the random event as be throwing a coin. The follwoing is my code:
    Dim P as single
    Dim n as long, m as long
    For q = 1 to 100
    P = Rnd
    If p<0.5 Then
    n = n +1
    Label1.Caption = n 'n is number that coin heads after it fall down
    Else
    m = m +1
    Label2.Caption = m
    End If
    Next q
    But both sides(front and back) of a coin are equal only q is less than 100.Why? Thanks a lot.
    xuliangchu

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Try this
    Code:
    Private Sub Command1_Click()
    Dim P As Single, q As Integer
    Dim n As Long, m As Long
    For q = 1 To 100
        'without Randomize you are not getting a random number
        Randomize   
         P = Rnd
        If P < 0.5 Then
            n = n + 1
            'n is number that coin heads after it fall down
            Label1.Caption = n 
        Else
            m = m + 1
            Label2.Caption = m
        End If
    Next
    End Sub
    Last edited by Hack; 11-25-2010 at 08:44 AM.

Similar Threads

  1. Event hook causing memory leak
    By AKRichard in forum C++
    Replies: 32
    Last Post: 08-11-2010, 09:13 AM
  2. Attatch a event function to an element
    By greffin in forum AJAX
    Replies: 0
    Last Post: 02-28-2007, 06:46 AM
  3. DataAdapter RowUpdated Event Problem (C# 2.0)
    By me_code2004 in forum .NET
    Replies: 1
    Last Post: 06-07-2006, 08:52 PM
  4. How long before the next version??
    By _CAG in forum .NET
    Replies: 146
    Last Post: 08-12-2002, 10:40 PM
  5. Re: App Object (fixes)
    By Rob Teixeira in forum .NET
    Replies: 129
    Last Post: 06-06-2002, 05:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center