CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2006
    Posts
    47

    How to end an rnd() function call !!!

    hi all,
    I am in need of a solution to exit from a random no. generation process !!!.

    Actually, I need to generate a sequence of random numbers between two limits
    say between 1 and 30. For this im using the rnd() function call like this..

    Randomize 1
    'generate random number

    iRndNum = Int((20 * Rnd) + 1)

    This is producing random numbers between 1 and 20. But the proble is how to make the function not to generate the same number twice and
    is there any way to find the end of number generation.
    How can i get to know that all the numbers between 1 and 20 are generated without repetition.

    Again the two things i need accomplish is,
    1. The same number should not be generated twice.
    2. The End of number generation between two limits..

    please help me out..

    thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How to end an rnd() function call !!!

    You need a second array. Look at this LOTTERY example. It counts occurences of each number drawn.
    Code:
    Option Explicit
    Dim a(99, 5) As Integer
    Dim picked(5) As Integer
    
    Private Sub Form_Load()
    Dim txt As String
    Dim x As Integer, y As Integer
    Randomize
    Call loadvalues
    'For x = 0 To 99
    '  For y = 0 To 5
    '	txt = txt & a(x, y) & ", "
    '  Next y
    '  txt = Left(txt, Len(txt) - 2) & vbCrLf
    ' Next x
    ' MsgBox txt
     
    Dim arr(48) ' or max numbers - 1 in ticket
    Dim cnt As Integer, cnteach As Integer
    For cnt = 0 To 48
      arr(cnt) = 0
      For x = 0 To 99
    	For y = 0 To 5
    	  If a(x, y) = cnt Then
    		arr(cnt) = arr(cnt) + 1
    	  End If
    	Next y
      Next x
    Next cnt
    For x = 0 To 48
      List1.AddItem Format(arr(x), "00") & "	 -   " & x
    Next x
    txt = "Count  Num" & vbCrLf
    For x = 48 To 42 Step -1
      txt = txt & List1.List(x) & vbCrLf
    Next x
    MsgBox txt
    End Sub
    
    Sub loadvalues()
      Dim b As Integer
      Dim c As Integer
      Dim arr(49) As Integer
      Dim flag As Boolean
      Dim pickednum As Integer
      For b = 0 To 99
    	For c = 0 To 5
    	  picked(c) = 0
    	Next c
    	For c = 0 To 5
    	  Do
    		pickednum = Int(Rnd * 49) + 1
    		flag = CheckPicked(b, pickednum, c)
    	  Loop Until flag = True
    	Next c
      Next b
    End Sub
    
    Function CheckPicked(f As Integer, x As Integer, c As Integer) As Boolean
      Dim d As Integer
      For d = 0 To c
    	If a(f, d) = x Then
    	  CheckPicked = False
    	  Exit Function
    	End If
      Next d
      a(f, c) = x
      CheckPicked = True
    End Function
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Mar 2006
    Posts
    47

    Re: How to end an rnd() function call !!!

    Thanks dglienna..
    You've given a good point to thinks about..

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

Featured