You are here:

Advanced Math/Calculating Random Numbers

Advertisement


Question
QUESTION: I have been wanting to create a computer program that produces random numbers based on various parameters. Now disregarding that a computer can only produce a quazi random number, and in fact the concept of random might not refer to anything, I need help in some cases. For example if I asked for the initial random number from the computer it will be a number between 0 and 1 which I will multiply out to a whole number and cut off all decimals giving me a number dividable by 10. However if I wanted to then program it to give me a random number from 1 to 26 for example how would I do that? I mean I could tell it to select a random number repeatedly until it selects a number within that range by mistake, but ultimately wouldn't it still be a 1/100 or 1/000 chance (depending on what the random number is multipied out to) of selecting those numbers? It seems like it would be uneven odds even if it has no choice but to select between 1 and 26.

ANSWER: A true place for generating random numbers can be found at http://www.random.org/

To generate a random number from 1 to 26, take the one off the computer times 26,
add 1, and chop off the remaining digits.

To generate a number out of a string of N numbers with the low value being A
and the step being B, so the high number is A+(N-1)B, do the following:

Generate a random number between 0 and 1, then call it X.

Multiply X by N, so now the value is in [0,N)
where [ means 0 is possible, but ) means N is not.

Truncate the value, so now it is an integer in [0,1,2,...,N-2,N-1).

Multiply it by B, so then it is in the set [0,B,2B,3B, ...(N-2)B, (N-1)B).

Add A to it, putting the number in the right range.


---------- FOLLOW-UP ----------

QUESTION: I am familar with that website. But my program needs to be stand alone, and much more flexible than that site. So you're say that if I got a random number between 1 and 100 and wanted a number between 1 and 26 I would multiply what I got by 26 and add 1? Say I got 54. 54x26+1=1405. Now what would I do to reduce that back into the range of between 1 and 26? Divide out 100 and round any decimals? Why add 1? I have no idea what an "N" number is so half your message alludes me. lol.

ANSWER: If the number were desired to be between 1 and 26, don't multiply by 26,
just take mod 26 and add 1.

To reduce that number back in the range of 1 to 26, do a mod 26.

Let's say the number needs to be 1 and 8.

A better set of numbers can be found, but I will use 5 and 1.
The first number will be taken as 1.
Each time, we multiply by 5 and add 1.
1*5+1 = 5+1 = 6, and mod 8 is still 6;
6*5+1 = 30+1 = 31, and mod 8 is 7;
7*5+1 = 35+1 = 36, and mod 8 is 4;
4*5+1 = 21, mod 8 is 5;
5*5+1 = 26, mod 8 is 2;
2*5+1 = 11, mod 8 is 3;
3*5+1 = 16, mod 8 is 0;
0*5+1 = 1, mod 9 is 1;
here, the series would repeat.

So the list of genernated numbers is 1, 6, 7, 4, 5, 2, 3, 0, 1.

These are very small numbers to work with, but this is so the operations are easily understood.
It the numbers were suppose to be between 1 and 8, add 1 to each of the results, but keep the base number the same for next time.  The output would be 2, 7, 8, 5, 6, 3, 4, 1, 2.

One of the major criteria for selecting larger numbers is that they have no factors in common with each other.  For example, if the large number were 100, it is known that 100 = 2*2*5*5.
That means the other two factors (the multiplier and the adder) could not be divisible by 2 or 5.  There needs to be some condition such that each number is random enough from the last number, but that's beyond what I know.


---------- FOLLOW-UP ----------

QUESTION: What is "mod?"

Answer
The term 'mod' is used in mathematics to mean the remainder after division.
---------------------------------------------------------------
For example, lets look at 25 mod 7.  The largest ineger to multiply 7 by is 3, for 7*3=21,
which is less than 25.  Note that that next higher integer, 4, gives 4*7 = 28,
and that is too high.

Since 7*3 = 21, and we have 25, the answer to 25 mod 7 is 4.
---------------------------------------------------------------
If we look at n mod 2, the answer will always be 0 or 1.
If n is even, the answer is 0; if n is odd, the answer is 1.
---------------------------------------------------------------
Suppose we have 47 mod 13.  13*1 = 13, and that is less than 47;
13*2 = 26, and that is less than 47;
13*3 = 39, and that is less than 47; and
13*4 = 52, but that is greater than 47.

Since 4 gives a number too high, 3 is how many times to multiply 13 by.
This 3 is not the answer, but 3*13 = 39, and 47 - 39 = 8,
so the answer is 8,  This says 47 mod 13 = 8.
----------------------------------------------------------------
N,k,N mod k
1, 5, 1, since 5 doesn't go;
2, 5, 2, since 5 doesn't go;
3, 5, 3, since 5 doesn't go;
4, 5, 4, since 5 doesn't go;
5, 5, 0, since 5 divides evenly;
6, 5, 1, since 6-5=1;
7, 5, 2, since 7-5-2;
8, 5, 3, since 8-5=3;
9, 5, 4, since 9-5=4;
10, 5, 0, since 5 divides evenl;
11, 5, 1, since 11-2*5=11-10=1.

There are three examples with three different ways to explain it.

Advanced Math

All Answers


Answers by Expert:


Ask Experts

Volunteer


Scott A Wilson

Expertise

I can answer any question in general math, arithetic, discret math, algebra, box problems, geometry, filling a tank with water, trigonometry, pre-calculus, linear algebra, complex mathematics, probability, statistics, and most of anything else that relates to math. I can even tell you it takes me over 2,000 steps to go a mile, but is that relevant?

Experience

Experience in the area; I have tutored people in the above areas of mathematics for almost two years in AllExperts.com. I have tutored people here and there in mathematics since before I received a BS degree almost 25 years ago. In just two more years, I received an MS degree as well, but more on that later. I tutored at OSU in the math center for all six years I was there. Most students offering assistance were juniors, seniors, or graduate students. I was allowed to tutor as a freshman. I tutored at Mathnasium for well over a year. I worked at The Boeing Company for over 5 years. I received an MS degreee in Mathematics from Oregon State Univeristy. The classes I took were over 100 hours of upper division credits in mathematical courses such as calculus, statistics, probabilty, linear algrebra, powers, linear regression, matrices, and more. I graduated with honors in both my BS and MS degrees. Past/Present Clients: College Students at Oregon State University, various math people since college, over 7,500 people on the PC from the US and rest the world.

Publications
My master's paper was published in the OSU journal. The subject of it was Numerical Analysis used in shock waves and rarefaction fans. It dealt with discontinuities that arose over time. They were solved using the Leap Frog method. That method was used and improvements of it were shown. The improvements were by Enquist-Osher, Godunov, and Lax-Wendroff.

Education/Credentials
Master of Science at OSU with high honors in mathematics. Bachelor of Science at OSU with high honors in mathematical sciences. This degree involved mathematics, statistics, and computer science. I also took sophmore level physics and chemistry while I was attending college. On the side I took raquetball, but that's still not relevant.

Awards and Honors
I earned high honors in both my BS degree and MS degree from Oregon State. I was in near the top in most of my classes. In several classes in mathematics, I was first. In a class of over 100 students, I was always one of the first ones to complete the test. I graduated with well over 50 credits in upper division mathematics.

Past/Present Clients
My clients have been students at OSU, people nearby, friends with math questions, and several people every day on the PC, and you're probably make one more.

©2012 About.com, a part of The New York Times Company. All rights reserved.