Random numbers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
Thank a lot Arvin,
After I copy and paste the following code into a new module, how can I run
it? (sorry for my newbbie question)
Thanks again

Ο χÏήστης "Arvin Meyer [MVP]" έγγÏαψε:
 
Thanks a lot.

It works perfectly

Ο χÏήστης "Arvin Meyer [MVP]" έγγÏαψε:
 
Can someone help ?

I did the below. It worked perfectly. I opened a blank database and created
a new module and copied and pasted the VB code.

My database does not have any tables, forms, queries or report as of now. I
want to use thsi database only for sample number generation, wherein I will
create a table, where i will input start number and end number. I want the
sample numbers to be shown on a form. I do not knwo, anything about VB.

I am working in an audit firm, and i need to generate sample numbers on a
frequent basis. Please help.

Thanks.
 
In continuation to the below email, i will also need a field, where I will
specify the sample size. For eg., i need 30 numbers to be generated or 50
numbers to be generated.
 
Can someone help ?

I did as stated before using the VBA code. It worked perfectly.

I opened a blank database and created a new module and copied and pasted the
VB code.

My database does not have any tables, forms, queries or report as of now. I
want to use thsi database only for sample number generation, wherein I will
create a table, where i will input start number and end number. I want the
sample numbers to be shown on a form. I do not knwo, anything about VB.
Also, I want the user to input the number of records to be selected.

I am working in an audit firm, and i need to generate sample numbers on a
frequent basis. Please help.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top