Random data

A

Al

Hi I have 1500 records, and in one field I want to insert at random one of
six options. Is there an easy way to do this?

Thanks
Al
 
M

Marshall Barton

Al said:
Hi I have 1500 records, and in one field I want to insert at random one of
six options. Is there an easy way to do this?


there's probably is an easy way, but I don't know what you
mean by "options".

If all you want is a to set the field to a random selection
of 0,1,2,3,4,5, then use an Update query:

UPDATE [tablename] SET [thefield] = CInt(6*Rnd(somefield])

Make sure that [somefield] only contains numbers greater
than zero (an AutoNumber field is good for this).
 
R

Ron Weiner

Al

Here is "A" way

Write a function that randomly returns one of your six values.

Function RandVal(Dummy As Long) As Integer
RandVal = Int((6 * Rnd) + 1)
End Function

Then write a update query that call the function.

UPDATE tblSomeTable SET RandColumn = RandVal([PrimaryKey])

Note that we are passing a dummy value to the to the RandVal function. If
your table has an autonumber primary key you can use this value. If you do
not pass this dummy value then the query optimizer will only call the
function once, and use the same value to update ALL of the rows. When
passing the dummy value the optimizer is forced to call the function for
each row.


Ron W
 

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

Top