Save some time - Random Numbers

Discussion in 'C#' started by jaymcc, Feb 3, 2006.

  1. #1
    Hi

    I use this very regularly to generate a set of random numbers in a range. It will do things like find 10 random numbers between 1 and 100 as an example.

    function JRandomNumbers(intFrom,intTo,IntCount)
     Dim Nums(), I, J, ArraySize, IsUsed, NextNum
     
     ArraySize = intCount - 1
     
     Redim Nums(ArraySize)
     
     For i = 0 to ArraySize
      Randomize
      IsUsed = True
      
      Do Until IsUsed = False
       NextNum = Int((intTo - intFrom + 1) * Rnd() + intFrom)
       IsUsed = BeenUsed(Nums, NextNum)
      Loop
      Nums(i) = NextNum
     Next
     JRandomNumbers = Nums
    End Function
    Function BeenUsed(intNums, intValue)
     'Response.write "<br>Checking for the number " & intValue & ", "
     Dim k, Used
     
     Used = False
     
     for k = 0 to ubound(intNums)
      'Response.write "Pos " & k & " = " & intNums(k) & ", " 
      If intNums(k) = intValue Then
       Used = True
       'Response.write " FOUND "
      end if
     next
     
     BeenUsed = Used
    End Function
    
    Code (markup):
    Enjoy!

    J
     
    jaymcc, Feb 3, 2006 IP