Results 1 to 5 of 5
  1. #1
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2011
    Posts
    214

    rnd() Function


    Quick question: does the rnd() function only work with AutoNumbers? Or does it work with everything?

    Thanks.

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,405
    Here's an example of randomizing a double.

    Code:
    Function fcnGenRandom(arg As Double) As Double
        Randomize
        fcnGenRandom = ((arg) * Rnd + 1)
    End Function
    Last edited by davegri; 03-06-2020 at 09:53 AM. Reason: format

  3. #3
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    Numbers only. To use it with text you need to randomize the ascii or keyboard character numbers.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    You can make it work with alpha, dates etc by using an underlying numeric range.

    Here's one for random alpha string of X characters

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : randomalpha
    ' Author    : jack
    ' Date      : 12/10/2015
    ' Purpose   : Returns random alpha characters based on your HowManyChars value
    '---------------------------------------------------------------------------------------
    '
    Function randomalpha(HowManyChars As Integer) As String
              Dim A As Long
              Dim Z As Long, i As Long
              Dim str As String
              Dim ralpha As Variant
              Dim Result As String
    10       On Error GoTo randomalpha_Error
    
    20        A = 1
    30        Z = 26
    40        str = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
    50        ralpha = Split(str, " ")
    
    60        For i = 1 To HowManyChars
    70            Result = Result & ralpha(randomNumber(A, Z) - 1)
                  'Debug.Print Result
    80        Next i
    90        randomalpha = Result
    
    100      On Error GoTo 0
    110      Exit Function
    
    randomalpha_Error:
    
    120       MsgBox "In Line " & Erl & "  Error " & Err.Number & " (" & Err.Description & ") in procedure randomalpha  of Module ABC"
    End Function
    A Random number function:

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : randomNumber
    ' Author    : Jack
    ' Created   : 11/18/2010
    ' Purpose   : To Generate Random numbers between and including a range of numbers.
    'Lo and Hi are the lowest and highest random numbers you wish to generate.
    '
    'The Randomize keyword is critical to getting different results for each Access session.
    '=======================================================================================
    '---------------------------------------------------------------------------------------
    ' Last Modified:
    '
    ' Inputs: N/A
    ' Dependency: N/A
    '------------------------------------------------------------------------------
    '
    Function randomNumber(Lo As Long, Hi As Long) As Long
    10       On Error GoTo randomNumber_Error
    
    20    Randomize
    30    randomNumber = Int((Hi - Lo + 1) * Rnd + Lo)
    
    40       On Error GoTo 0
    randomNumber_Exit:
    50       Exit Function
    
    randomNumber_Error:
    
    60        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure randomNumber of Module AccessMonster"
    70        GoTo randomNumber_Exit
               
    End Function
    Here is a test routine.

    Code:
    Sub testRandomAlpha()
     Debug.Print randomalpha(20)
    End Sub

  5. #5
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2011
    Posts
    214
    Thank you, everyone. I will take a look and see if I can utilize any of your suggestions.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 15
    Last Post: 10-17-2018, 09:26 PM
  2. Replies: 2
    Last Post: 02-26-2017, 11:31 AM
  3. Replies: 3
    Last Post: 03-04-2016, 10:36 AM
  4. Replies: 8
    Last Post: 11-04-2014, 10:44 PM
  5. Replies: 8
    Last Post: 01-31-2014, 01:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums