Post Reply 
An unexpected result involving sums of random numbers
03-02-2023, 10:00 PM
Post: #14
RE: An unexpected result involving sums of random numbers
(03-01-2023 07:14 PM)Namir Wrote:  Your results are good. With one million iteration I get an average of 2.71. With a sum of 2 I get an average of 4.6. And with an average of 3 I get an average of 6.5.

I have modified two programs to handle arbitrary sums. Even 5000 iterations, which is quite feasible on the HP-15C LE, will give results close to the theoretical values:

1 -> 2.7182818; e

2 -> 4.6707743; e(e - 1)

3 -> 6.6665656; e(2e² - 4e + 1)/2


Code:

   001-    42 21 11   f LBL A
   002-       44 25     STO I
   003-          34     x<>y
   004-          20     ×
   005-       44  0     STO 0
   006-           0     0
   007-       44  1     STO 1
   008-       43 36   g LSTΧ
   009-    42 21  0   f LBL 0
   010-       42 36   f RAN#
   011-    42 21  1   f LBL 1
   012-          34     x<>y
   013-    44 40  1     STO+ 1
   014-          34     x<>y
   015-       42 36   f RAN#
   016-          40     +
   017-    43 30  8   g TEST 8    ; x<y
   018-       22  1     GTO 1
   019-          33     Rv
   020-    42  5 25   f DSE I
   021-       22  0     GTO 0
   022-       45  1     RCL 1
   023-    45 40  0     RCL+ 0
   024-    45 10  0     RCL/ 0
   025-       43 32   g RTN


0 STO RAN# 1 ENTER 5000 f A -> 2.7152 ( 55.5 seconds )

0 STO RAN# 2 ENTER 5000 f A -> 4.6656 ( 100.6 seconds )

0 STO RAN# 3 ENTER 5000 f A -> 6.6754 ( 146.7 seconds )


Of course, on a computer or smartphone running Free42 we can go even further. For instance,


Code:
00 { 40-Byte Prgm }
01▸LBL "X"
02 X<>Y
03 RCL× ST Y
04 STO 00
05 CLX
06 STO 01
07 X<> ST L
08▸LBL 00
09 RAN
10▸LBL 01
11 X<>Y
12 STO+ 01
13 X<>Y
14 RAN
15 +
16 X<Y?
17 GTO 01
18 R↓
19 DSE ST Y
20 GTO 00
21 RCL 01
22 RCL+ 00
23 RCL÷ 00
24 END


4 SEED 1 ENTER 1E7 XEQ “X” -> 2.7181530 ( 29.4 seconds )

4 SEED 2 ENTER 1E7 XEQ “X” -> 4.6711803 ( 50.1 seconds )

4 SEED 3 ENTER 1E7 XEQ “X” -> 6.6667329 ( 71.6 seconds )


As mentioned earlier in this thread, I was motivated by a short note on a book I read some years ago and ran into it again last week, which read

Quote:Here is an example of e turning up unexpectedly. Select a random number between 0 and 1. Now select another and add it to the first. Keep doing this, piling on random numbers. How many random numbers, on average, do you need to make the total greater than 1? The answer is e.

Not forgetting about Valentin's Math Challenge linked above would have spared me some time, but then again I would have lost the chance to dust off my calculators and check that out for myself. Detailed information and a shorter and possibly faster RPN program also available in his original solution.

Thanks you all for your interest and contributions.

Gerson.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: An unexpected result involving sums of random numbers - Gerson W. Barbosa - 03-02-2023 10:00 PM



User(s) browsing this thread: 5 Guest(s)