George said:
Hello everybody again,
Is there any possibility to generate random numbers in MS-Access, either
using a query or a module?
e.g (30 random numbers between 1 and 500)
Thank you in advance
Here's one you can use that was posted in a newsgroup by M.G. Foster some
time ago. I use it occasionally, so I know it works fine:
Function testGetRandom()
' test getRandomNumbers function
Const lo = 0 ' Low boundary of population
Const hi = 500 ' High boundary of population
Const itms = 30 ' Number of items to return
Dim X As Variant
X = getRandomNumbers(lo, hi, itms)
Dim i As Integer
For i = lo To hi
If X(i) = True Then Debug.Print i
Next i
End Function
Function getRandomNumbers(lo As Integer, hi As Integer, toSelect As Integer)
' Purpose:
' Return an indicated number of unique random numbers from
' a defined population.
' In:
' lo The bottom number in the population
' hi The top number in the population
' e.g: lo = 73, hi = 250
' toSelect The number of items between lo & hi to return.
' Out:
' Variant array of booleans
' If the item is selected item(i) = True
' If the item isn't selected item(i) = False
' Created:
' mgf 25may99
ReDim items(lo To hi) As Variant
Dim selected As Integer
' Seed the randomizer
Randomize
' Generate the array of unique, random items
Do While selected < toSelect
Dim rec As Integer
' Get a number between lo and the hi boundaries
' *From the VBA Help file on Rnd()*
rec = Int((hi - lo + 1) * Rnd + lo)
' If the item hasn't been marked, mark it.
If items(rec) = False Then
items(rec) = True
' Keep track of the number of items selected.
selected = selected + 1
End If
Loop
getRandomNumbers = items
End Function
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads
http://www.datastrat.com
http://www.mvps.org/access