Results 1 to 3 of 3
  1. #1
    DallasW is offline Novice
    Windows 8 Access 2013
    Join Date
    Feb 2015
    Posts
    8

    Randon Number Generator

    I have a table called RNDT in which I need to have a number generated between 1 and 30 daily. In the table, I have the date set as the key field and then another field dailyrndn for the daily random number. I am having problems with RND() as it is updating all entries in the query with the same number and not saving to the table or it creates an entry into the table, but does not generate a random number.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Here's a routine that should help.

    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.
    '''''' from techonthenet
    '---------------------------------------------------------------------------------------
    ' Last Modified:
    '
    ' Inputs: N/A
    ' Dependency: N/A
    '------------------------------------------------------------------------------
    '
    Function randomNumber(Lo As Integer, Hi As Integer) As Integer
    10       On Error GoTo random_Error
    20    Randomize
    30    randomNumber = Int((Hi - Lo + 1) * Rnd + Lo)
    
    40       On Error GoTo 0
    50       Exit Function
    
    random_Error:
    
    60        MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure random of Module AccessMonster"
    End Function
    Test routine
    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : mytestOfRandomNumber
    ' Author    : Jack
    ' Date      : 11/18/2010
    ' Purpose   : Create 10 random numbers between 3 and 300
    '---------------------------------------------------------------------------------------
    '
    Sub mytestOfRandomNumber()
              Dim a As Integer
              Dim z As Integer
              Dim i As Integer
    10        a = 3
    20        z = 300
    30        For i = 1 To 10
    40            Debug.Print i & "   " & randomNumber(a, z)
    50        Next i
    End Sub
    Sample run:
    1 29
    2 132
    3 201
    4 54
    5 39
    6 4
    7 109
    8 92
    9 222
    10 129

  3. #3
    InsuranceGuy is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Colorado
    Posts
    126
    Try passing a non-static number (or date) into the random function in the query.

    Instead of =RND() use =RND([date field])

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

Similar Threads

  1. Rnd function error (Random Number generator)
    By SiciliandoBlue in forum Access
    Replies: 11
    Last Post: 09-02-2015, 03:06 AM
  2. random number generator
    By scottdg in forum Programming
    Replies: 22
    Last Post: 05-12-2015, 01:36 PM
  3. Number generator without Autonumber?
    By Megood in forum Programming
    Replies: 7
    Last Post: 07-19-2012, 08:02 PM
  4. Account ID - Generator
    By sschrader1 in forum Queries
    Replies: 4
    Last Post: 04-09-2012, 08:09 AM
  5. Sorta Random Serial Number Generator
    By Cuselco in forum Programming
    Replies: 3
    Last Post: 08-27-2010, 12:05 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