473,386 Members | 1,741 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,386 software developers and data experts.

Random Question generator for MS Access

I am use the MS Access Form as frontend and ms-access Database as backend
I want to retrive 10 question randomly from database out of 100 question
simultenously in richtextbox. If i click on next button then second question display.
I am use the four radio button for option
and I click on finish button then show the result and how iam store the 10 question with user answer in database.
Nov 24 '12 #1
14 9334
hi mohammad how are u?

I give u the select query to retrieve the top 10 records of a table,
I think the remaining would be so easy for u to do,

good luck
Expand|Select|Wrap|Line Numbers
  1. SELECT TOP 10 table1.id, table1.title, Rnd(id) AS Expr1
  2. FROM table1
  3. ORDER BY Rnd(id) DESC;
Nov 24 '12 #2
zmbd
5,501 Expert Mod 4TB
MohammadShabbir


- Mojyzzzz's solution will not prevent duplicate questions so it is advisable to check for these or use the "DISTINCT" and "M" has made some assumptions about your table design so be aware of the fact that you may well have to modify the query. ( SQL Predicates )

- As this is a somewhat familiar question often asked for homework or class projects I would ask/require that you post your current code and DB design. Note: that does not mean attach pictures or files as this information can be typed into the post quite easily.
Please remember to format your code and SQL blocks by using the <CODE/> button in the toolbar.
Nov 25 '12 #3
NeoPa
32,556 Expert Mod 16PB
You should also bear in mind that the suggested SQL will randomise the data for you, but it will be a repeatable randomisation, such that you will get the same random ten records each time it's run.
Nov 25 '12 #4
zmbd
5,501 Expert Mod 4TB
Thank you NeoPa, my oversite on SQL. I usually use VBA and open a recordset for this type of operation when creating my quizes for work.
Nov 25 '12 #5
If you want to prevent duplication, the only way is to create a table which stores any question ID that is seen by the user,

or the simple but not flexible way is to add a field that works as a flag that represents whether the question is seen or not
Nov 25 '12 #6
zmbd
5,501 Expert Mod 4TB
mojyzzzz
I assure you that there are easier ways than either method you've cited to achieve a random record set without duplication.
In fact, one of the very first threads I was involved with at this site dealt with the question of random record selection using weighted probabilities ( https://bytes.com/showthread.php?t=940752 ) which ran into just such a discussion.

If there is one thing I have learned over the years is to NEVER say that the "only" way to do some given task is to do it this way or that way.
Nov 25 '12 #7
TheSmileyCoder
2,322 Expert Mod 2GB
NeoPa
You should also bear in mind that the suggested SQL will randomise the data for you, but it will be a repeatable randomisation, such that you will get the same random ten records each time it's run.
I would venture that its a bit more complicated then that. The seed number only returns the same result if the seed number is negative,
Example
Expand|Select|Wrap|Line Numbers
  1. Rnd(-1)=rnd(-1)
  2. rnd(1)<>rnd(1)
OR the seed has been reset just prior to running the random number generator each time.
Nov 25 '12 #8
Rabbit
12,516 Expert Mod 8TB
It should also be noted that Rnd(1) in a select query will return the same number for every row. That is because the function is run only once because the SQL engine assumes a function call with a static parameters will always return the same result.

The only way to get different numbers for each row is to vary the input by incorporating a field value.
Nov 25 '12 #9
NeoPa
32,556 Expert Mod 16PB
@Smiley & Rabbit
Well done lads. Both very good points. My post was in error and I overlooked the issue that Rabbit brought up related to SQL optimisations whereby it will determine that the function needn't be re-called as the parameters are the same. It essentially treats it as a constant value once called the first time.
Nov 26 '12 #10
ADezii
8,834 Expert 8TB
My approach is similar to zmbd's in that I always use the Recordset approach for generating x number of Random Records for a Data Set of y Records. The general Logic is as follows (using 10 Random Records out of 100):
  1. Generate 10 Random Numbers between 1 and 100, storing each in an Array.
  2. Loop thru the Array checking for Duplicate Values.
  3. Should there be Duplicate Value(s), generate another Random for that Slot.
  4. Repeat this process until all 10 Randoms are now Unique.
  5. We now have 10, Unique, Random Numbers stored in an Array which now act as Record Pointers to the 100 Record Data Set.
  6. Now we can use some mechanism to Navigate to each Record position defined by the Array, and retrieve the appropriate Values.
Nov 26 '12 #11
zmbd
5,501 Expert Mod 4TB
I'm afraid I cheat even further than that... I use a class module and use the primary key as the name of the instance. I got the idea from reading thru an example where students and rooms were being handled. At the time it really didn't make much sense when tables would do the job - but then I had a need for the quiz... I was playing with Class modules... already had the jack-hammer out of the toolshed.

The other way I've done this is using a listbox on a form (oh yes, I went there) and then do the loop thingy to check for duplicates much like an array.

In either method... I still open a record set in VBA and pull the random records.
Nov 26 '12 #12
NeoPa
32,556 Expert Mod 16PB
What I do is to create an array of integers that reflect all the record numbers (or record offsets) that exist in the dataset. Each element of the array is initially populated with a number matching its own position (IE. Element[0]=0; element[1]=1; element[2]=2; etc). N is set as the number of records.

Now, for each iteration through the loop, a random number (R) is generated such that 0 <= R < 1. R is multiplied by N and the integer portion taken (NOT rounded but truncated.), giving a random integer (X) between 0 and 9 on the first iteration through the loop. Importantly, element[N] is then swapped with element[X]. Lastly, X is decremented (X = X - 1). The last two steps ensure that only unselected values are available for selection next time through the loop.
Nov 26 '12 #13
zmbd
5,501 Expert Mod 4TB
That's slick... I've done somthing like that with two list boxes... moved the element from one to the second... just for user touchy-feely :-)
Nov 26 '12 #14
NeoPa
32,556 Expert Mod 16PB
Thanks Z. I originally came up with this approach when looking for a way to shuffle cards before dealing (hence the swapping of the elements rather than the simpler moving of [N] to [X]). I struggled to come up with something that would work and not be clumsy, but I only came up with this as a fully formed solution after waking up one day.
Nov 26 '12 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Trythat | last post by:
This might seem a stupid question but here goes - I wrote a key generator for one of my programs in Visual Basic, so I recieve sales notification via email and then use my program to generate the...
21
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
14
by: kittikun | last post by:
Hi all, I was wondering if there is a random generator that is "reversible". I need to go back in time in my application and still need to use deterministic random numbers. Most of RNG implement...
3
by: adonis | last post by:
hello. i need some help with a c or c++ program. a program for generating random numbers with exponential and poisson distribution. please sombody help. thanks.
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
4
by: Ruhi22 | last post by:
I am trying to use the default random generator in C. the funcion rand() is fine but when tried 'srand(time(0))' it is giving me the following error: error C3861: 'time': identifier not found...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
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,...

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.