Stuck on understanding the solution

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

Guest

Can anyone help me understand the solution for the question titled "Assign
Random Number to a Record"? I'm new at arrays in Access and have been trying
to figure out how to make this work for several hours now - no luck. Can
someone help?

Thanks.
 
I'm new at arrays in Access
I do not think Access has arrays but what you asked has nothing to do with
arrays.
Random Number to a Record"?
Is this a homework question?
 
When set to autonumber random it placed incremental numbers in the field for
each record. Would I have to delete and re-import the data for this to work
correctly? How would I make sure the random numbers are >0 and <21?
Thanks
 
Hi Rod,

In addition to the sage responses...

If I had a table and I wanted to add
a field that had random numbers >0
and <21, I would add following function
to a code module (say "modRandom")

'************ Code Begin ***********
'Code courtesy of
'Joe Foster
'http://www.mvps.org/access/queries/qry0011.htm
Function Randomizer () As Integer
Static AlreadyDone As Integer
If AlreadyDone = False Then Randomize : AlreadyDone = True
Randomizer = 0
End Function
'************ Code End *************

Then I would add a field (say "fRnd") of type Long
to my table (say "yurtable") which also has some other
field (say "somefield"),
then run following update query

UPDATE yurtable
SET [fRnd] = Int(20 * Rnd(1 + Len([somefield] & "")) + 1)
WHERE randomizer()=0;

from Access Help:
*** quote ***

Rnd() returns a random number of type SINGLE.

You could use an upperbound and lowerbound to get random integers in a
given range.

RandNum = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

*** unquote ***
so...

substituting upperbound=20 and lowerbound=1,

[fRnd]=Int((20 -1 + 1) * Rnd() + 1)

Rnd() needs a non-negative, non-zero, non-null argument
or else all our results would be the same.

So we feed it the length of some field (plus make sure it is
non-negative, non-zero, non-null) from the same record we
are computing our random number:

[fRnd]=Int((20 -1 + 1) * Rnd(1 + Len([somefield] & "")) + 1)

all that's left is to reduce the math...

good luck (maybe w/your homework?),

gary
 
Thanks Gary. It's the details you provided which will not only lead me to
resolving this problem, but also increase my knowledge - that's exactly what
I needed!

Gary Walter said:
Hi Rod,

In addition to the sage responses...

If I had a table and I wanted to add
a field that had random numbers >0
and <21, I would add following function
to a code module (say "modRandom")

'************ Code Begin ***********
'Code courtesy of
'Joe Foster
'http://www.mvps.org/access/queries/qry0011.htm
Function Randomizer () As Integer
Static AlreadyDone As Integer
If AlreadyDone = False Then Randomize : AlreadyDone = True
Randomizer = 0
End Function
'************ Code End *************

Then I would add a field (say "fRnd") of type Long
to my table (say "yurtable") which also has some other
field (say "somefield"),
then run following update query

UPDATE yurtable
SET [fRnd] = Int(20 * Rnd(1 + Len([somefield] & "")) + 1)
WHERE randomizer()=0;

from Access Help:
*** quote ***

Rnd() returns a random number of type SINGLE.

You could use an upperbound and lowerbound to get random integers in a
given range.

RandNum = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

*** unquote ***
so...

substituting upperbound=20 and lowerbound=1,

[fRnd]=Int((20 -1 + 1) * Rnd() + 1)

Rnd() needs a non-negative, non-zero, non-null argument
or else all our results would be the same.

So we feed it the length of some field (plus make sure it is
non-negative, non-zero, non-null) from the same record we
are computing our random number:

[fRnd]=Int((20 -1 + 1) * Rnd(1 + Len([somefield] & "")) + 1)

all that's left is to reduce the math...

good luck (maybe w/your homework?),

gary


Rod said:
When set to autonumber random it placed incremental numbers in the field
for
each record. Would I have to delete and re-import the data for this to
work
correctly? How would I make sure the random numbers are >0 and <21?
Thanks
 
Back
Top