473,320 Members | 2,048 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,320 software developers and data experts.

Questions on Randomising

doma23
107 100+
Hey guys!
I'm newbie and I'm trying to understand how this works.
Everything is clear and only thing I don't understand is why the order of the newly generated random numbers is random?
If these are the generated random numbers: 3,10,5,2
Why the order is not 2,3,5,10 instead of random?
Tnx!

**Edit**
This thread was split from a related thread at How to delete a record from recordset but not from table.
Jul 27 '10 #1

✓ answered by Stewart Ross

... and here's a link to a thread that ADezii and I answered which is similar to the original approach in the thread from which this was split, and which shows the use of a public function supplied with a dummy value to force re-evaluation of the Rnd() function:

http://bytes.com/topic/access/answer...y-79th-records

-S

11 1716
NeoPa
32,556 Expert Mod 16PB
@doma23
I guess this would be down to the nature of random numbers and sequences thereof. If it were predictable that the next number in the sequence were greater than the previous, then that very predicatability would preclude the concept of randomness.
Jul 27 '10 #2
doma23
107 100+
I'm not sure you understood me well.
After calling randomizer function I have this query:
Number Random
1 63
2 321
3 801
4 102
5 200

And now, if I'm going to sort it by RANDOM ASCENDING, I should have this:
Number Random
1 63
4 102
5 200
2 321
3 801

Number column would still be always random.
Instead, it seems it doesn' matter whether I choose ASCENDING OR DESCENDING, as long as I choose SORT BY it will sort RANDOM by some unknown rule.
That's what I don't understand.
Or you wanted to tell me that Access recognizes wheter the number were generated by using rnd function, and therefore when SORT BY is included it just sorts those numbers randomly?

The other thing I don't understand is why hype261 put the "* number" in randomizer function: randomizer = Rnd() * 100 * number ?
Futhermore, if there is a "* number", how come the function never returns numbers higher than 999?
If the rnd() function gives a random number from 0 to 10, ie. let's say it returns 6 for the number 4, than it should be randomizer = 6 * 100 * 4 = 2400.

Hope I was clear, and sorry if I'm adding to the topic whose author already found solution, but I really find this interesting and I would like to know how it really works?
Thanks!
Jul 28 '10 #3
NeoPa
32,556 Expert Mod 16PB
I think I understand a little better now.

I have no idea why the Random column doesn't seem to sort correctly.
Rnd() returns a real value which is between 0 and 1. The range of numbers is always handled by multiplication by the range it should span. IE. A random number between 15 & 20 is (Rnd() * 5) + 15.
I have no idea why Number was included within the original code.

We probably need to leave this here or split into a new thread. Let me know if you're interested in continuing further.
Jul 28 '10 #4
hype261
207 100+
@NeoPa
The reason I included Number in the original code was because if I didn't the randomizer function seemed to be evaluated only once for the whole query which wouldn't give a random order at all.

I believe why the order by is messed up is because Access is evaluating the randomizer twice for each row. Once for the column and once in the order by function. I could be wrong on this one though.
Jul 28 '10 #5
doma23
107 100+
@hype261
Hmm...that's weird because I'm getting random number only with Rnd()*100. Actually, I believe that even just plain "rnd()" is enough.

For your point that it might be because Access evaluates it twice, I guess it might be true. That satisfies my hunger for explanation. :)
Jul 28 '10 #6
hype261
207 100+
@doma23
If I did the following query...

Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.pk, Table1.text
  2. FROM Table1
  3. ORDER BY Rnd();
The results would always be the same. This leads me to believe that Rnd is only being called once for the entire query. Though if I do...

Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.pk, Table1.text
  2. FROM Table1
  3. ORDER BY Rnd([pk]);
I get random values and according to MSDN...
Rnd[(number)]

If number is greater than zero the next random number in the sequence.
Jul 28 '10 #7
NeoPa
32,556 Expert Mod 16PB
Now you can explore the issue to your hearts' content :)
Jul 28 '10 #8
Stewart Ross
2,545 Expert Mod 2GB
When Access evaluates any function supplied as a calculated field in a query, unless it is forced to re-evaluate the function for every row in the query it is evaluated just the once. You can verify this by including in a test query an unbound calculated field with a call to the date/time function Now(), for example. You will see if you do so that the calculated field returns the same date/time value for all rows in the query until you re-run the whole query once again.

This behaviour is quite different to having a bound field in a table - say [Update Time] - with a default value of Now(). The function is evaluated for each row when there is such a dependency.

From the point of view of the query engine, if a function call 'stands alone' and has no dependency on field values for the current row, why re-evaluate it each time?

As you have found with Rnd(), supplying a row-dependent value to the function (if such an argument is allowed) forces re-evaluation for each row.

-Stewart
Jul 28 '10 #9
Stewart Ross
2,545 Expert Mod 2GB
... and here's a link to a thread that ADezii and I answered which is similar to the original approach in the thread from which this was split, and which shows the use of a public function supplied with a dummy value to force re-evaluation of the Rnd() function:

http://bytes.com/topic/access/answer...y-79th-records

-S
Jul 28 '10 #10
doma23
107 100+
@Stewart Ross Inverness
Thank you very much Stewart.
I've read the other topic and you perfectly explained the issue there.
Jul 28 '10 #11
NeoPa
32,556 Expert Mod 16PB
It was a bright day indeed when Stewart joined our ranks.

Nice explanation :)
Jul 29 '10 #12

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

Similar topics

10
by: WindAndWaves | last post by:
Dear All I am making a quiz with a four-option-multiple-choice scenario. When you enter the quiz, you provide one answer and three wrong answers. Then the contestant opens the quiz and I...
0
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for...
0
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
4
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
8
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
1
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.