Associated Topics || Dr. Math Home || Search Dr. Math

Assigning Random Numbers

```
Date: 05/16/2000 at 09:21:10
From: Ande Bennett
Subject: Randomization

I am using a programming language and have a random number generator
that can generate a random number of 0, 1, or 2. I need to assign
those three values to 4, 12, and 14. I am looking for a formula to do
this. For example, if I needed a random number of 4 or 12, I could
use:

random(2) * 8 + 4

but I don't know how to do three numbers. Any ideas?
```

```
Date: 05/16/2000 at 17:15:59
From: Doctor TWE
Subject: Re: Randomization

Hi Ande - thanks for writing to Dr. Math.

Try this function. It's weird, but it works:

Let x = random number (0, 1, or 2)

F(x) = 14 - 2*x^(log_2(5))

where log_2(5) is the base-2 logarithm of 5 (~= 2.321928095). For this
function, F(0) = 14, F(1) = 12 and F(2) = 4. Here's why this function
works:

x =  0   1   2
----------------
x^log_2(5) =  0   1   5 <- see how x^log_2(5) converts 2 -> 5
2*x^log_2(5) =  0   2  10
14-2*x^log_2(5) = 14  12   4

I hope this helps. If you have any more questions, write back.

- Doctor TWE, The Math Forum
http://mathforum.org/dr.math/
```
Associated Topics:
High School Functions

Search the Dr. Math Library:

 Find items containing (put spaces between keywords):   Click only once for faster results: [ Choose "whole words" when searching for a word like age.] all keywords, in any order at least one, that exact phrase parts of words whole words

Submit your own question to Dr. Math
Math Forum Home || Math Library || Quick Reference || Math Forum Search