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

Multiplying Random Numbers

Hi, I am trying to determine if it is possible to multiply two fields that I have generated in Access using the random function. The result always seems to be another random number which uses the same range that I have for the two previous fields.

Ex. Expr 1: Int((6-3+1)*Rnd()+3)*100
Expr 2: Int(((2-1+1)*Rnd()+1))*3

I want to be able to return the result of the numbers that were generated using these, but if I try the straight forward:

Expr 3: [Expr 1]*[Expr 2] it just returns a random number that uses the same range as those numbers.
Dec 29 '11 #1
14 2257
Stewart Ross
2,545 Expert Mod 2GB
What you have posted is not really something that makes much sense, particularly when you have retained expressions such as (6-3+1) which should have been simplified out.

Expr 1 simplifies to 100 * Int(3 + (4 * Rnd)), which will generate a random number in the range 300 - 700. Expr 2 Is just 3 * Int(1 + (2 * Rnd)), which will generate a random number in the range 3 - 9. Multiplying the two expressions will generate a random number in the range 900 - 6300, which could have been generated by a single expression in any event.

The Rnd function will generate new random numbers on every occasion it is called - these will not be the same as in the individual expressions, so (Expr 1 * Expr 2) will not be the same as the product of the two original calculations. As the range of the random number generated will be within the bounds stated above (900 - 6300), what's the problem?

The ranges mentioned must have some significance in whatever you are doing, but you haven't explained what they mean or why you need these range boundaries.

-Stewart
Dec 29 '11 #2
NeoPa
32,556 Expert Mod 16PB
Interesting, and I would add surprising (to me at least). I would have expected the resultant values to be saved and reused by the SQL engine rather than simply saving the formulas to be re-evaluated. Certainly interesting.
Dec 29 '11 #3
ADezii
8,834 Expert 8TB
@Stewart:
Forgive me if I am mistaken, but aren't your Upper Ranges off, since Rnd() can never return a Value of 1:
Expand|Select|Wrap|Line Numbers
  1. 100 * Int(3 + (4 * Rnd)) 
  2. Lower Range: 300
  3. Upper Range: 600
Expand|Select|Wrap|Line Numbers
  1. 3 * Int(1 + (2 * Rnd))
  2. Lower Range: 3
  3. Upper Range: 6
Dec 29 '11 #4
NeoPa
32,556 Expert Mod 16PB
You're not mistaken ADezii. It means that Int(Rnd() * X) always returns an integer value in the range of X integer values starting from zero (EG. Where X = 4 then the range of values is 0, 1, 2 and 3).

This detail doesn't change the purport of what Stewart was saying, but is nevertheless an important point to consider when generating code for random integers within any range. Using Int() is also a good way to ensure that each possible integer in the range has an equal occurrence probability (and not all code that uses Rnd() does).
Dec 29 '11 #5
Stewart Ross
2,545 Expert Mod 2GB
@ADezii - darn, mistakenly forgetting that the range for Rnd does not include 1.0 itself!

@NeoPa, the Rnd() function recalculating whenever it is called is also a feature of the implementation of the similar Rand() function in Excel. It can result in some interesting side effects. For example, if a sort is applied to a set of random numbers generated by placing =Rand() in each cell, the values in the cells change completely but the end result (at least as displayed) is not in sorted order at all. This is because the random numbers are recalculated during the process (after the sort, from the testing I've done).

-Stewart
Dec 29 '11 #6
NeoPa
32,556 Expert Mod 16PB
Weird stuff - but it makes sense unfortunately :-(
Dec 29 '11 #7
Thanks for the help guys. So given everything you all have said, the short answer is no correct?

I was hoping to be able to use Access to generate some random numbers for something I'm working on. I was hoping to be able to also have the check numbers based on those random numbers, but it seems like I'll have to find a work around for that now.

I didn't simplify it out because I have been changing the boundaries based on the results that are generated and what I expect, and it's easier for me to just put in the upper and lower boundaries rather than doing the math - especially when the DB will do it itself. Expr 1 is the value for a period, Expr 2 is the number of periods, and I wanted Expr 3 to be the total value.
Dec 29 '11 #8
NeoPa
32,556 Expert Mod 16PB
Jabari:
I didn't simplify it out because
That makes some sense within your project, but not really any when posting a question for people to help you with.

All is not lost. This is a (very rare) situation where it makes sense to add your data into a temporary table held specifically for this task. Once the values have been created they can then be processed from this table with the clear understanding that no further calls will be made to recalculate the values as you try to use them.

Make sense?
Dec 29 '11 #9
I'm not a programmer but it does make sense. I don't know how to do that right now, but I'll look into it and see how it comes out.
Dec 29 '11 #10
Got it to work - did what you said, created a table, then turned the query into an append query. So I can run a query off that table and come out with the total cost. And now all I have to do is run a macro that deletes the data and then runs the append query each time I want to generate new data.

You're a GENIUS!
Dec 29 '11 #11
NeoPa
32,556 Expert Mod 16PB
Very kind of you to say so, but it seems you're not too much of a slacker yourself if you moved on to a solution with just that hint.

I'm not a big fan of Access' macro system myself, but VBA code can do the same thing of course with much better control. The deletion can be done with a simple DELETE query and the appending can be done with an INSERT INTO query. Let us know if you struggle with either as, it seems, this is somewhat new to you.
Dec 30 '11 #12
That hint was really the solution I was looking for. I've done the delete and append queries that can generate that data for me.

You can take a look at my next question Can I generate a random number of sales this way? and tell me what you think about that. I have no idea how to go about it at this point in time.
Dec 30 '11 #13
Mihail
759 512MB
Why do not use only a Make table query instead all this additions and deletions ?
Dec 30 '11 #14
NeoPa
32,556 Expert Mod 16PB
Make table queries have a habit of getting out of control. They're fine for one-off situations, but when you are working within a database and doing the same work repeatedly you typically want control over the design of what you're working with. Also, you don't want the additional overhead of deleting and creating the same table every time. It saves you nothing whatsoever and adds to your overhead while reducing your control. Deleting and appending records is a far more appropriate solution.

The alternative of creating tables without deleting them when done with should not even be contemplated.
Dec 30 '11 #15

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

Similar topics

10
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
5
by: cvnweb | last post by:
I am trying to generate 2 random numbers that are diffrent, in order to add them to existing numbers to generate numbers that start out the same, but are randomly added and subtracted so that they...
10
by: Leon | last post by:
I know by default the random number generator use the time, but what is the best seed I can used in my web application? The Program generate 6 unique random numbers and load each of them in a...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
11
by: TreatmentPlant | last post by:
I need to generate a few thousand true random numbers using C++. I have some code now that does alright, but when you plot the results on a graph, you notice patterns, so the numbers are not...
9
by: Cogito | last post by:
I would like to calculate factorial numbers that produce results of say 100 digits. What is the best way of doing it in Javascript? Can I define a variable and somehow have control on each of its...
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
5
by: DAXU | last post by:
Hi, I need to generate a fixed number of random integers that their summary equals to certain values. For example, the scenario can be: simulate user hits (e.g. 20000) within a hour (3600...
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: 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...
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
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
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...
0
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...
0
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,...

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.