Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,35 @@
begin % sleeping beauty problem - translated from the Wren sample, via Agena %
integer seed; % seed for random number generation %
% returns the next middle-square random number in [0..1) %
real procedure random ;
begin
if seed = 0 then seed := time( -1 ) rem 10000;
seed := ( ( seed * seed ) div 100 ) rem 10000;
seed / 10000
end random ;
real procedure sleepingBeauty( integer value reps ) ;
begin
integer wakings, heads;
wakings := heads := 0;
for i := 1 until reps do begin
wakings := wakings + 1;
if random < 0.5 then begin
heads := 1 + heads % [0..0.5) = heads %
end else begin
wakings := 1 + wakings % [0.5..1) = tails %
end if__random_lt_500000__
end for_i ;
write( i_w := 1, s_w := 0, "Wakings over ", reps, " repetitions = ", wakings );
( heads / wakings ) * 100
end sleepingBeauty ;
begin
real pc;
seed := 0; % initialise the random number generator %
pc := sleepingBeauty( 1000000 );
write( s_w := 0, "Percentage probability of heads on waking = ", pc, "%" )
end
end.

View file

@ -0,0 +1,32 @@
with Ada.Numerics.Discrete_Random;
with Ada.Text_IO; use Ada.Text_IO;
procedure Sleeping_Beauty is
type Coin is (Heads, Tails);
package Random_Coin is new Ada.Numerics.Discrete_Random (Coin); use Random_Coin;
package FIO is new Float_IO (Float);
Tosser : Generator;
Probability : Float;
function Experiment (Tosses : Integer) return Float is
Awakenings, Heads_Count : Integer := 0;
begin
for Iteration in 1 .. Tosses loop
case Random (Tosser) is
when Heads =>
Awakenings := Awakenings + 1;
Heads_Count := Heads_Count + 1;
when Tails =>
Awakenings := Awakenings + 2;
end case;
end loop;
Put_Line ("Awakenings over" & Tosses'Image & " iterations:" & Awakenings'Image);
return Float (Heads_Count) / Float (Awakenings) * 100.0;
end Experiment;
begin
Reset (Tosser);
Probability := Experiment (1_000_000);
Put ("Percentage probability of heads on waking:");
FIO.Put (Probability, 3, 5, 0);
end Sleeping_Beauty;

View file

@ -0,0 +1,18 @@
scope # sleeping beauty problem - translated from the Wren sample, via Algol 68
local proc sleepingBeauty( reps :: number ) :: number
local wakings, heads := 0, 0;
to reps do
wakings +:= 1;
if math.random() < 0.5 then # default range is [0..1)
heads +:= 1 # [0..0.5) = heads
else
wakings +:= 1 # [0.5..1.0) = tails
fi
od;
printf( "Wakings over %d repetitions = %d\n", reps, wakings );
return ( heads / wakings ) * 100
end;
printf( "Percentage probability of heads on waking = %10.6f%%\n", sleepingBeauty( 1_000_000 ) )
epocs

View file

@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define N 1000000
bool coin_toss() {
return rand() & 1;
}
double run_experiment(int amount) {
int wakings = 0;
int wakings_heads = 0;
for (int i=0; i<amount; i++) {
bool heads = coin_toss();
/* Wake up on Monday */
wakings++;
if (heads) {
wakings_heads++;
continue;
}
/* Wake up on Tuesday */
wakings++;
}
return (double)wakings_heads/(double)wakings;
}
int main(void) {
double chance = run_experiment(N);
printf("Chance of waking up with heads: %f\n", chance);
return 0;
}

View file

@ -0,0 +1,15 @@
iterations = 10_000_000
wakings = heads = 0
r = Random.new
iterations.times do
wakings += 1
if r.next_bool # heads?
heads += 1
else
wakings += 1
end
end
puts heads/wakings

View file

@ -1,6 +1,6 @@
reps = 1e6
for i to reps
coin = random 2
coin = random 1 2
wakings += 1
if coin = 1
heads += 1

View file

@ -0,0 +1,18 @@
(do ;;; sleeping beauty problem - translated from the Wren sample, via Pluto
(fn sleeping-beauty [reps]
(var (wakings heads) (values 0 0))
(for [_ 1 reps]
(set wakings (+ 1 wakings))
(if (< (math.random) 0.5)
(set heads (+ 1 heads)) ;; [0..0.5) = heads
;else
(set wakings (+ 1 wakings)) ;; [0.5..1.0) = tails
)
)
(print (.. "Wakings over " reps " repetitions = " wakings))
(* (/ heads wakings) 100)
)
(print (.. "Percentage probability of heads on waking = " (sleeping-beauty 1_000_000)))
)

View file

@ -14,7 +14,6 @@
140 END
150 REM interview if the coin came up heads
160 AWAKE = AWAKE + 1
170 NHEADS = NHEADS + 1
180 GUESS = INT(RND*2)
190 IF GUESS = HEADS THEN CHEADS = CHEADS + 1 ELSE WTAILS = WTAILS + 1
200 RETURN

View file

@ -0,0 +1,18 @@
do -- sleeping beauty problem - translated from the Wren sample, via Pluto
local function sleepingBeauty( reps )
local wakings, heads = 0, 0
for _ = 1, reps do
wakings = wakings + 1
if math.random() < 0.5 then
heads = heads + 1 -- [0..0.5) = heads
else
wakings = wakings + 1 -- [0.5..1.0) = tails
end
end
print( "Wakings over " .. reps .. " repetitions = " .. wakings )
return ( heads / wakings ) * 100
end
print( "Percentage probability of heads on waking = " .. sleepingBeauty( 1000000 ) )
end

View file

@ -0,0 +1,27 @@
10 DUMMY=RANDOM(!TIMECOUNT)
20 MONDAY = 0 : TUESDAY = 1: CHEADS = 0: WTAILS = 0: AWAKE = 0
30 HEADS = 0 : TAILS = 1: WHEADS = 0: CTAILS = 0: COIN = INT(RND*2)
40 FOR SB = 1 TO 300000
50 IF COIN = HEADS THEN GOSUB 160 ELSE GOSUB 210
60 COIN = INT(RND*2) : NEXT SB
70 OPEN "OUTPUT_BEAUTY.TXT" FOR WIDE OUTPUT AS #F
80 PRINT #F, "Sleeping Beauty was put through this experiment ";SB-1;" times."
90 PRINT #F, "She was awoken ";AWAKE;" times."
100 PRINT #F, "She guessed heads ";CHEADS+WHEADS;" times."
110 PRINT #F, "Those guesses were correct ";CHEADS;" times. ";100*CHEADS/(CHEADS+WHEADS);"%"
120 PRINT #F, "She guessed tails ";WTAILS+CTAILS;" times."
130 PRINT #F, "Those guesses were correct ";CTAILS;" times. ";100*CTAILS/(CTAILS+WTAILS);"%"
140 CLOSE #F: REPORT EVAL$(BUFFER("OUTPUT_BEAUTY.TXT"))
150 WIN "NOTEPAD", DIR$+"OUTPUT_BEAUTY.TXT": END
160 REM interview if the coin came up heads
170 AWAKE++
180 GUESS = INT(RND*2)
190 IF GUESS = HEADS THEN CHEADS++ ELSE WTAILS++
200 RETURN
210 REM interviews if the coin came up tails
220 FOR DAY = MONDAY TO TUESDAY
230 AWAKE++
240 GUESS = INT(RND*2)
250 IF GUESS = HEADS THEN WHEADS++ ELSE CTAILS++
260 NEXT DAY
270 RETURN

View file

@ -0,0 +1,18 @@
// sleeping beauty problem - translated from the Wren sample, via Pluto
sleepingBeauty = function( reps )
wakings = 0
heads = 0
for _ in range( 1, reps )
wakings += 1
if rnd < 0.5 then // rnd returns a randome number in [0..1)
heads += 1 // [0..0.5) = heads
else
wakings += 1 // [0.5..1.0) = tails
end if
end for
print "Wakings over " + reps + " repetitions = " + wakings
return ( heads / wakings ) * 100
end function
print "Percentage probability of heads on waking = " + sleepingBeauty( 1000000 )

View file

@ -0,0 +1,18 @@
do -- sleeping beauty problem - translated from the Wren sample, via Agena
local function sleepingBeauty( reps : number ) : number
local wakings, heads = 0, 0
for _ = 1, reps do
wakings += 1
if math.random() < 0.5 then
heads += 1 -- [0..0.5) = heads
else
wakings += 1 -- [0.5..1.0) = tails
end
end
print( $"Wakings over {reps} repetitions = {wakings}" )
return ( heads / wakings ) * 100
end
print( $"Percentage probability of heads on waking = { sleepingBeauty( 1_000_000 ) }" )
end

View file

@ -0,0 +1,44 @@
program sleeping_beauty;
$ amount of times to run the experiment
const N = 1000000;
$ seed RNG
setrandom(0);
chance := simulate(N);
print("Chance of waking up with heads:", chance);
$ Run the experiment N times
proc simulate(amount);
state := {};
state.wakings := 0;
state.heads := 0;
loop init i := 0; step i +:= 1; until i = amount do
run_experiment(state);
end loop;
return state.heads / state.wakings;
end proc;
$ Run the experiment once
proc run_experiment(rw state);
heads := coin_toss();
$ monday - wake up
state.wakings +:= 1;
if heads then
state.heads +:= 1;
return;
end if;
$ tuesday - wake up if tails
state.wakings +:= 1;
end proc;
$ Toss a coin
proc coin_toss();
return 1 = random 1;
end proc;
end program;