473,770 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random Number in Query?

Hello All,

Is it possible to create multiple random numbers in a query where
there are numerous records?

I've created a custom function. When I use it in a query it creates
the same random number for ALL the records. It appears the function is
only getting called once, therefore only one random number is being
generated for all the records. I want a different random number for
each record. Is this possible, or do I have to do it all in code and
add the random number as data to a field in the table?

What I did is below. At least the short version:

Query:

SELECT tblInvCurYr.ID, ReturnRndCGSPer c(1) AS InvCGSPerc FROM
tblInvCurYr;

Random Function in Code:

Function ReturnRndCGSPer c(dblCGSperc As Double)
Randomize 'initialize random number generator
dblCGSperc = (0.8 - 0.6) * Rnd + 0.6
ReturnRndCGSPer c = dblCGSperc
End Function
TIA!

--
Regards,

Greg Strong
Nov 13 '05 #1
4 8249
Hi Greg,

I found the same as well. Having a different value for dblCGSperc, this
will correct the issue, for example another field that is different for
each row would do the trick. The question remains - why is this the
case? Not really sure! I did notice that the initial values were
different from the final values. What am I talking about (not really
sure sometimes)? I notice that for record 1 the value as 0.63. After
completion it was 0.71. NB: These aren't actually the values is saw,
just an example.

Hope this helps...

Regards,
Dave
Greg Strong wrote:
Hello All,

Is it possible to create multiple random numbers in a query where
there are numerous records?

I've created a custom function. When I use it in a query it creates
the same random number for ALL the records. It appears the function is
only getting called once, therefore only one random number is being
generated for all the records. I want a different random number for
each record. Is this possible, or do I have to do it all in code and
add the random number as data to a field in the table?

What I did is below. At least the short version:

Query:

SELECT tblInvCurYr.ID, ReturnRndCGSPer c(1) AS InvCGSPerc FROM
tblInvCurYr;

Random Function in Code:

Function ReturnRndCGSPer c(dblCGSperc As Double)
Randomize 'initialize random number generator
dblCGSperc = (0.8 - 0.6) * Rnd + 0.6
ReturnRndCGSPer c = dblCGSperc
End Function
TIA!

--
Regards,

Greg Strong


Nov 13 '05 #2
Greg Strong <NoJunk@NoJunk4 U².com> wrote:
: Hello All,

: Is it possible to create multiple random numbers in a query where
: there are numerous records?

: I've created a custom function. When I use it in a query it creates
: the same random number for ALL the records. It appears the function is
: only getting called once, therefore only one random number is being
: generated for all the records. I want a different random number for
: each record. Is this possible, or do I have to do it all in code and
: add the random number as data to a field in the table?

: What I did is below. At least the short version:

: Query:

: SELECT tblInvCurYr.ID, ReturnRndCGSPer c(1) AS InvCGSPerc FROM
: tblInvCurYr;

: Random Function in Code:

: Function ReturnRndCGSPer c(dblCGSperc As Double)
: Randomize 'initialize random number generator

You reintialize the random number generator each time it's
called. Apparently 'randomize' makes the same
initialization each time. Even if that weren't so, you
don't want to initialize your generator each time you
choose a random, so take the randomize step out of the
function and run it once, before you begin your select.
--thelma

: dblCGSperc = (0.8 - 0.6) * Rnd + 0.6
: ReturnRndCGSPer c = dblCGSperc
: End Function
: TIA!

: --
: Regards,

: Greg Strong

Nov 13 '05 #3
On 22 Sep 2005 18:50:13 GMT, Thelma Lubkin <th****@alpha2. csd.uwm.edu>
wrote:
You reintialize the random number generator each time it's
called.
Ok. So why is this wrong?
Apparently 'randomize' makes the same
initialization each time. Even if that weren't so, you
don't want to initialize your generator each time you
choose a random, so take the randomize step out of the
function and run it once, before you begin your select.


Well I want a different random number each time. I passed the ID to the
random function and it created a different number each time.

--
Regards,

Greg Strong
Nov 13 '05 #4
Greg Strong <NoJunk@NoJunk4 U².com> wrote:
: On 22 Sep 2005 18:50:13 GMT, Thelma Lubkin <th****@alpha2. csd.uwm.edu>
: wrote:

:> You reintialize the random number generator each time it's
:> called.

: Ok. So why is this wrong?
If you reintialize each time, you're telling it
to begin at the same place in the randomization
algorithm as it did the time before, so it chooses
the same random number: remember, these aren't truly
'random': they are repeatable computations if you begin
at the same initial conditions--and you're asking it to
restore the initial conditions every time you ask for
a number.

Apparently 'randomize' makes the same

:> initialization each time. Even if that weren't so, you
:> don't want to initialize your generator each time you
:> choose a random, so take the randomize step out of the
:> function and run it once, before you begin your select.

: Well I want a different random number each time. I passed the ID to the
: random function and it created a different number each time.

The 'random function' doesn't include the randomization
step. That step is used to *initialize* the random sequence
that you will generate with repeated calls to the random
function. If you initialize again you reset the
algorithm to again begin at the first random number in
this sequence. There is yet another function
that you can use to reset the entire sequence so that
the random function gets you a different set of numbers.

--thelma
: Regards,

: Greg Strong
Nov 13 '05 #5

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

Similar topics

2
6285
by: Ini | last post by:
Hi, Is there an easy way to pick at random one record out of a recordset and then leave this recordset? I was thinking about doing a recordcount of the recordset, then find at random the number between 1 and <recordcount> and then move to this record in the recordset. There is a easier way, isn't it?
1
2482
by: David Barger | last post by:
Greetings, It appears that an Append Query I run in Access XP is randomly failing to append a field. I have payroll data being entered into a payroll database. This data is exported daily to two csv files. (One for updated data, and the other for unupdated data.) The CSV files are attached to my Jobcosting database. After the CSV
8
5723
by: Locky | last post by:
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
2
4845
by: IceCube | last post by:
Hello, I would like to select/filter at random 30 records out of an Access-table of 1500 records. I know the option "Top" which gives me the possibility to see the 30 first records of the table The problem is that this option is linked to the way of sorting the table. So you will always select from a very restraint part of the 1500 records. Is there a possibility * to select or sort at random in Access. Maybe there exists an
4
2396
by: darrel | last post by:
I can grab a random number in vb.net like this: Dim RandomClass As New Random Dim RandomNumber As Integer RandomNumber = RandomClass.Next(1, 26) However, what I want is a random number. Short of making a case statement with 26 options, is there a more streamlined way to get an integer between 1-26 translated into one of the letters?
26
3159
by: Jimmy | last post by:
ill have a database with 1 table and 3 fields: ID FIRSTNAME LASTNAME (the ID field will be the auto incrementing index) there might be 10 records in the DB, there might be 10,000. i need to open the DB and randomly select a record (and then display the name, which i dont have a problem with) how can i randomly select a record? im guessing id have to open a recordset
6
5478
by: InnoCreate | last post by:
Hi everyone. I've recently written a classic asp website which uses an MS Access datasource. I know this is less than an ideal data source as it has limited functionality. I have a search form on my website which allows users to define parameters and return results accordingly. The problem i have is a need to return these results in a random order each time. With SQLServer i know NEWID() would do the trick - used this many times before...
3
3885
by: John Fairhurst | last post by:
Hi, The following code should select the specified number of records randomly from the database <% .... query = "SELECT FROM " Set RS = Server.CreateObject("ADODB.Recordset")
5
8157
by: muskie | last post by:
I've looked through as many posts about this as possible, but all end with no resolution. I simply need records from a table in random order, and I will be calling this recordset in a SQL statement from ASP. I've tried the following but it does not produce random order: SELECT * FROM Table1 ORDER BY Rnd(TableID) ASC; where TableID is an autonumber field. I've seen references to Randomize, but how do you use Access's
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.