473,624 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random Selection of Records

I'm trying to do a search under a set criteria followed by a selection of
random entries meeting this criteria. But I don't seem to be able to
achieve this.

The idea being to search on say subject and then select a random set of
records meeting that subject.

Any ideas or thought would be helpful. I'm using Access XP to try this out.

TIA

Keith
Jul 17 '05 #1
4 6280
> I'm trying to do a search under a set criteria followed by a selection of
random entries meeting this criteria. But I don't seem to be able to
achieve this.

The idea being to search on say subject and then select a random set of
records meeting that subject.

Any ideas or thought would be helpful. I'm using Access XP to try this

out.

I'm not a database person myself, so I'll leave the specifics of
implementing this idea to you. What I am thinking is to find out how many
records were return from you search, assign the record numbers to an array
of integers, shuffle that array of record numbers into a random order, and
then pick out the first X number of record numbers (you decide on how many
random entries you want) in order to index back into the set of records that
was returned from you search. For example, if your search returned, say,
record numbers 2, 19, 20, 39 and 50, then set up this array

Dim NumberOfReturne dRecords As Long
Dim RecNums() As Long
' Search your database and get the returned records
NumberOfReturne dRecords = <<RecordCount , 5 for this example>>
ReDim RecNums(1 To NumberOfReturne dRecords)
RecNums(1) = 2
RecNums(2) = 19
RecNums(3) = 20
RecNums(4) = 39
RecNums(5) = 50

then shuffle the contents around using the subroutine found at the end of
this message

RandomizeArray RecNums

and, finally, use the first, say, 3 entries (for this example) to obtain the
record numbers of 3 random items from your search set.

Rick - MVP

The following is a generalized "shuffling" routine that I've posted in the
past. Give it an array of elements and it will put them in random order and
return the randomized elements back in the original array that was passed to
it. It only visits *each* array element *once* so it is quick. The code
takes care of running the Randomize statement one time only (which is all
that is necessary).

Sub RandomizeArray( ArrayIn As Variant)
Dim X As Long
Dim RandomIndex As Long
Dim TempElement As Variant
Static RanBefore As Boolean
If Not RanBefore Then
RanBefore = True
Randomize
End If
If VarType(ArrayIn ) >= vbArray Then
For X = UBound(ArrayIn) To LBound(ArrayIn) Step -1
RandomIndex = Int((X - LBound(ArrayIn) + 1) * _
Rnd + LBound(ArrayIn) )
TempElement = ArrayIn(RandomI ndex)
ArrayIn(RandomI ndex) = ArrayIn(X)
ArrayIn(X) = TempElement
Next
Else
'The passed argument was not an array
'Put error handler here, such as . . .
Beep
End If
End Sub
Jul 17 '05 #2
Check http://www.mvps.org/access/queries/qry0011.htm at "The Access Web"

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(No private e-mails, please)

"Keith Griffiths" <gr************ *@hotmail.com> wrote in message
news:c3******** **@titan.btinte rnet.com...
I'm trying to do a search under a set criteria followed by a selection of
random entries meeting this criteria. But I don't seem to be able to
achieve this.

The idea being to search on say subject and then select a random set of
records meeting that subject.

Any ideas or thought would be helpful. I'm using Access XP to try this out.
TIA

Keith

Jul 17 '05 #3
Rick Rothstein wrote:

[snip]
then shuffle the contents around using the subroutine found at the end of
this message

RandomizeArray RecNums

and, finally, use the first, say, 3 entries (for this example) to obtain the
record numbers of 3 random items from your search set.

Rick - MVP


[snip]

It's also possible to randomly pick from an
array without shuffling all the elements:

1) choose a random element 0..n and use it.
2) swap the nth element with the chosen element.
3) restrict the next choice to 0..n-1
4) repeat

As fast or faster than a full shuffle, and
you'd end up with a shuffled array after n
iterations anyway.

Just thought I'd throw that out for the group.
Jul 17 '05 #4
"Keith Griffiths" <gr************ *@hotmail.com> wrote in
news:c3******** **@titan.btinte rnet.com:
I'm trying to do a search under a set criteria followed by a
selection of random entries meeting this criteria. But I
don't seem to be able to achieve this.

The idea being to search on say subject and then select a
random set of records meeting that subject.

Any ideas or thought would be helpful. I'm using Access XP to
try this out.

TIA

Keith


Create an expression in your query grid with some numeric value (an
autonumber is fine) mod (rnd()* someconstant (I like 77)+1)

Expr1: CLng(nz([seniority],123456)) Mod ((Rnd()*17)+1)

Set the criteria for this expression to =1

It will return some subset of the other criteria.

Bob Q.

Jul 17 '05 #5

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

Similar topics

3
3230
by: Andie | last post by:
Hello All, How would I go about using a disconnect recordset and select (x) records from it, x being the number of records to be selected. Many thanks in advance. -- Andie
5
5055
by: Jamie Fryatt | last post by:
Hi all, im a little bit new to all this so i you could help me a little i would be greatful. How do i select a completly random record from a database? sql = select (random) from tbl, sort of thing. Thanks for any help you can give Jamie
3
3190
by: jaYPee | last post by:
anyone know how can i get a 10% random records out of 2000 records? 2000 is only an example and it will vary anytime. i want to create a report out of this 10% thanks in advance
2
4837
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
2
1371
by: VB Programmer | last post by:
I am interested to hear your suggestions on this... I have a table full of survey questions. The questions are individually classified as priority 1, 2 or 3. (Priority 1 means the question shows up 3 times as much as the others, priority 2 means it shows up 2 times as much as priority 1.) I want to fill a temp table with a random selection of these questions. Example: If I want the temp table to contain 100 random "question" records...
2
1801
by: RJN | last post by:
Hi I have to randomly pickup records from database for some quality check of data. Say the data base has about 1000 records, I have to randomly select about 50 records. Can someone suggest me some simple Random generator algorithm to do this? Thanks RJN
19
3072
by: Boris Borcic | last post by:
does x.sort(cmp = lambda x,y : cmp(random.random(),0.5)) pick a random shuffle of x with uniform distribution ? Intuitively, assuming list.sort() does a minimal number of comparisons to achieve the sort, I'd say the answer is yes. But I don't feel quite confortable with the intuition... can anyone think of a more solid argumentation ?
3
6031
by: Cindy | last post by:
I'm trying to use the NEWID function in dynamic SQL and get an error message Incorrect syntax near the keyword 'ORDER'. Looks like I can't do an insert with an Order by clause. Here's the code: SELECT @SQLString = N'INSERT INTO TMP_UR_Randoms(Admit_DOCID, Client_ID, SelectDate, SelectType,RecordChosen)' SELECT @SQLString = @SQLString + N'(SELECT TOP ' + @RequFilesST + ' Admit_DOCID, Client_ID, SelectDate, SelectType, RecordChosen...
3
2186
by: Laphan | last post by:
Hi All I use a MySQL DB with my ASP classic web app. I've been asked if I can create a routine whereby I get a random number of products (records) from the DB and display these on the site. Basically every time a visitor hits the home page, they want the site to display a random selection of say 6 products. I have no problems getting the data and displaying it, my problem is
0
8177
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
8629
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
8341
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
8488
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...
0
7170
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1488
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.