# Random numbers, list operations, and math puzzles

February 27, 2000

by Pat McClellan

I need your help for this:

Create a random number for 1 to 999.

Create six random numbers p.ex. 2, 4, 5, 10, 25, 50

I need a code for combining these six numbers with math operations (+,-,*,/) to the result is the first number (1 to 999).

Is possible?

Pepe

Dear Pepe,

I'm not sure I understand exactly what you need to do, but random number generation and list operations are always a good topic for discussion. Ultimately I may not be able to help you, but maybe I can generate some discussion on the subject. For those of you who aren't familiar with this, we'll start with an overview of the basics of random number generation.

You can generate a random number by calling the random() function and supplying it with its upper limit. For example, if you want to generate a number between 1 and 10, just enter this in the message window:

put random(10) -- 2

If you want to use a random number in a Lingo script, you'll probably assign it to a variable. Something like this:

x = random(10)

When this command is executed, a number is assigned to x, and it will retain that value. But if you use the term random(10) again, it will generate a fresh number. For example:

x = random(10) y = random(10)

It is likely that x and y will have different values -- only a 1 in ten chance that they'll coincidentally share the same value.

Now, if you wanted to perform a math operation on these two numbers, you can do it like this:

x = random(10) y = random(10) sum = x + y put sum -- 13

In this example, we don't really know what the value of x or y is... just that they add up to 13. If we continue, we might be able to figure out their values.

difference = x - y put difference -- 1

Note that I didn't re-assign random values to x and y, so they have retained their previous values. We can tell from this last result that x is one more than y, so if their sum is 13, we know that x = 7 and y = 6.

mult = x * y put mult -- 42

Yep, that clinches it.

In your question, it seems that you might want to have more than 2 numbers. In this case, it might be a good idea to assign the values to a list. We can set up a repeat loop which will assign X number of values to a list.

on makeRandomList howManyValues, upperLimitrandomList = [] repeat with i = 1 to howManyValues x = random(upperLimit) add randomList, x end repeat return randomList end

I've put that into a movie script. Now, I'll call it from the message window. Let's say that we want 6 numbers in the list, and each number will be random(50) -- so our upperLimit is 50.

put makeRandomList (6, 50) -- [30, 41, 36, 28, 20, 8]

Easy. This makeRandomList script simply loops once for each number added to the list. Now let's do something with those values. I'll create a new handler that sums up the entire list:

on sum valueListthisSum = 0 repeat with i in valueList thisSum = thisSum + i end repeat return thisSum end

And in the message window:

valueList = makeRandomList(6, 50) put sum(valueList) -- 122

There are some other great things you can do with a list. You can simulataneously add, subtract, multiply or divide a number by each value in the list to give a new list. Try this:

initialList = [5,10,15,20,25] plus3List = initialList + 3 put plus3List -- [8, 13, 18, 23, 28] minus1List = initialList - 1 put minus1List -- [4, 9, 14, 19, 24] mult7List = initialList * 7 put mult7List -- [35, 70, 105, 140, 175] div2point3List = initialList/2.3 put div2point3List -- [2.1739, 4.3478, 6.5217, 8.6957, 10.8696]

Finally, you can apply math operation on one list from another.

initialList = [5,10,15,20,25] newList = makeRandomList (6, 50) put newList -- [8, 11, 42, 5, 34, 2]

We've now got 2 lists, initialList and newList. Let's add them together -- the first number of each list is added, then the second number in each list is added, etc.

put initialList + newList -- [13, 21, 57, 25, 59]

OK, enough with the list gymnastics. The one think I can't help you do is figure out a way to solve number puzzles. I think you're asking for an algorithm which can take six randomly generated numbers and figure out some combination of math operations on those numbers to come out with a previously selected random number. Something like this:

Answer = 163 NumberList = [4,13,50,150,214,325] 4 * (13 - (50/150)) + 325 - 214 = 163

Unfortunately, I don't know how to do such a thing. I'm not sure if it's even mathmatically possible, though I don't know how I would go about proving it to be impossible. It boggles my mind to think about the number of variations that would need to be checked. The order of the numbers, the groupings within parenthesis... I wouldn't know where to begin.

How about it readers? Any math gurus out there want to take a crack at this one? Post your comments and ideas, wild theories and philosophical formulas in the DOUGthreads forum for this article.

Copyright 1997-2017, Director Online. Article content copyright by respective authors.