473,382 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Insert Random Number

Sorry if this is a repost - my connection went as I was posting the
1st time!

I want to insert a random number, in mod 10, in a field, based on
whether another field is empty or not.

I am using the following query:

UPDATE tbl_Results SET Q11_17 = (Int((100000-50+1)*Rnd()+100000) Mod
10)*10
WHERE Q8_17<>'';

If I run this query, all fields in Q11_17 are updated with the SAME
random number if Q8_17 is not empty.

I want every field in Q11_17 that meets the criteria to have a NEW
random number, so my question is, how do I re-initialize the Rnd
function for each field?

Thanks
Nov 13 '05 #1
8 5697
Locky wrote:
Sorry if this is a repost - my connection went as I was posting the
1st time!

I want to insert a random number, in mod 10, in a field, based on
whether another field is empty or not.

I am using the following query:

UPDATE tbl_Results SET Q11_17 = (Int((100000-50+1)*Rnd()+100000) Mod
10)*10
WHERE Q8_17<>'';

If I run this query, all fields in Q11_17 are updated with the SAME
random number if Q8_17 is not empty.

I want every field in Q11_17 that meets the criteria to have a NEW
random number, so my question is, how do I re-initialize the Rnd
function for each field?

Thanks


Call a custom function, e.g.

function Random(Max, dummy)
randomize timer
random = rnd() * Max
end function

Send a field from the query as dummy parameter, this will ensure Access
calls the function for each record otherwise it will try to be eficient
and call it once for the entire query and plonk the same result into
each row.
Nov 13 '05 #2
Hi

Thanks for such a quick reply, but I am unsure as to how to implement
this in an access query.
Could you give me more info?
Sorry!!

Nov 13 '05 #3
ad*******@gmail.com wrote:
Hi

Thanks for such a quick reply, but I am unsure as to how to implement
this in an access query.
Could you give me more info?
Sorry!!


Something like: update table set field = Random(50)
Nov 13 '05 #4
That'll give every record the same number, Trevor. At least it did in
Access97. I don't think I've tried it since.

Cheap trick: Create an Autonumber field set to random... <Grin>

Or use a function that returns a random number after setting the
randomize value first.

Nov 13 '05 #5
c.*******@worldnet.att.net wrote:
Or use a function that returns a random number after setting the
randomize value first.


See the function 2 posts up, that's what it does, I just forgot the
second parameter on the calling line in the last post.
Nov 13 '05 #6

Trevor Best wrote:
c.*******@worldnet.att.net wrote:
Or use a function that returns a random number after setting the
randomize value first.


See the function 2 posts up, that's what it does, I just forgot the
second parameter on the calling line in the last post.


Indeed, except you don't want to do the "Randomize Timer" each pass
through the function. You only want to do it _once_.

Nov 13 '05 #7
c.*******@worldnet.att.net wrote:
Trevor Best wrote:
c.*******@worldnet.att.net wrote:

Or use a function that returns a random number after setting the
randomize value first.


See the function 2 posts up, that's what it does, I just forgot the
second parameter on the calling line in the last post.

Indeed, except you don't want to do the "Randomize Timer" each pass
through the function. You only want to do it _once_.


I've not tested, what is the overhead of that?
Nov 13 '05 #8
Sorry for the long delay in replying. It's been a week!

The problem with calling Randomize on each pass through the function
has to do with the way the "Random Number Generator" works. In fact,
it's not a generator at all, it's more like a lookup table. The
Randomize statement sets the initial lookup position, then each Random
call reads the value from the table and advances the "pointer" one
record forward.

Using the (optional) setting for the Randomize statement (in this case
Timer), tells the the Random Number Generator at what position to start
at. Giving it the same "time" (for simplicity's sake) re-sets the RNG
back to the same point over and over due to the speed at which queries
run at, thus the function will return the same "random" number over and
over.

Soneone in this newsgroup a while back posted one solution to this
problem which used a STATIC variable to help the function determine if
the Randomize statement had been used before or not, and if it hadn't
then it called the Randomize statement (using Timer). I remember
trying it, however the only way _I_ could get it to work was to call
the function before I ran the query, then let the query do it's thing.
I was doing the same thing using two seperate functions, so I didn't
save the code.

Nov 13 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jack | last post by:
I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr,int n) { // const int random_number_range=1000000;
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
16
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
4
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
3
by: tshad | last post by:
I have a page that I am getting a username and password as a random number (2 letters, one number and 4 more letters) I have 2 functions I call: *************************************************...
8
by: Anil Gupte | last post by:
I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0;...
0
by: sweatha | last post by:
Hi Friends I have designed a form with a label box named 'Label30' and text box named 'TextBox21'. Then I have generated the random number in the label box using the coding in form load event as ...
2
by: alishaikhji | last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example: - At one point, I need a random number (float) in the range 0.1 to 10.0...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.