Skip to content

Commit

Permalink
Added random_normal(mean,std) function to the lib
Browse files Browse the repository at this point in the history
  • Loading branch information
VForiel committed Oct 1, 2023
1 parent b7d589b commit dc95a4c
Show file tree
Hide file tree
Showing 5 changed files with 1,094 additions and 0 deletions.
35 changes: 35 additions & 0 deletions __global__/data/lib/functions/random_normal/gauss.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

# Compute arg = (x-m)²/(2σ²)
scoreboard players operation #gauss.arg tmp = #gauss.input tmp
scoreboard players operation #gauss.arg tmp -= #mean tmp
scoreboard players operation #gauss.arg tmp *= #gauss.arg tmp
scoreboard players set 1000 const 1000
scoreboard players operation #gauss.arg tmp *= 1000 const
scoreboard players set 2 const 2
scoreboard players operation #gauss.arg tmp /= 2 const
scoreboard players operation #gauss.arg tmp /= #std tmp
scoreboard players operation #gauss.arg tmp /= #std tmp

# Compute exp(arg)
execute store result storage bs:in math.exp.value double 0.001 run scoreboard players get #gauss.arg tmp
scoreboard players operation #math.exp bs.in = #gauss.arg tmp
function #bs.math:exp
execute store result score #math.exp bs.out run data get storage bs:out math.exp 1000

# Compute 1/(σ√(2π))
scoreboard players set #gauss.norm tmp 1000000
scoreboard players operation #gauss.norm tmp /= #std tmp
scoreboard players set 2507 const 2507
scoreboard players operation #gauss.norm tmp /= 2507 const

# Compute result -> 1/(σ√(2π)) / exp(arg)
scoreboard players operation #gauss.output tmp = #gauss.norm tmp
scoreboard players operation #gauss.output tmp *= 1000 const
scoreboard players operation #gauss.output tmp /= #math.exp bs.out

# tellraw @a [{"text":"x: ","color":"gray"},{"score":{"name":"#gauss.input","objective":"tmp"}}]
# tellraw @a [{"text":"arg: ","color":"gray"},{"score":{"name":"#gauss.arg","objective":"tmp"}}]
# tellraw @a [{"text":"1/exp(arg): ","color":"gray"},{"score":{"name":"#math.exp","objective":"bs.out"}}]
# tellraw @a [{"text":"norm: ","color":"gray"},{"score":{"name":"#gauss.norm","objective":"tmp"}}]
# tellraw @a [{"text":"gauss: ","color":"gray"},{"score":{"name":"#gauss","objective":"tmp"}}]
# tellraw @a [{"text":" "}]
14 changes: 14 additions & 0 deletions __global__/data/lib/functions/random_normal/interface.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

scoreboard players operation #x tmp = #mean tmp
scoreboard players operation #delta tmp = #std tmp
scoreboard players operation #delta tmp *= 2 const
scoreboard players set #loop tmp 0

# Computing starting prob
scoreboard players operation #gauss.input tmp = #x tmp
function lib:random_normal/gauss
scoreboard players operation #p_o tmp = #gauss.output tmp

function lib:random_normal/loop

tellraw @a [{"text":"Result:","color":"green","bold":true},{"score":{"name":"#x","objective":"tmp"}}]
39 changes: 39 additions & 0 deletions __global__/data/lib/functions/random_normal/loop.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@


# Computing step -> dx = (2*rand()-1) * delta
execute store result score #dx tmp run random value 0..2000
scoreboard players remove #dx tmp 1000
scoreboard players operation #dx tmp *= #delta tmp
scoreboard players operation #dx tmp /= 1000 const

# Getting new position -> new_x = x + dx
scoreboard players operation #new_x tmp = #x tmp
scoreboard players operation #new_x tmp += #dx tmp

# Computing new prob
scoreboard players operation #gauss.input tmp = #new_x tmp
function lib:random_normal/gauss
scoreboard players operation #p_n tmp = #gauss.output tmp

# Computing acceptance probability -> p = min(1, p_n / p_o)
scoreboard players operation #a tmp = #p_n tmp
scoreboard players operation #a tmp *= 1000 const
scoreboard players operation #a tmp /= #p_o tmp
scoreboard players operation #a tmp < 1000 const

# Deciding whether to accept or not -> if rand() < a: x = new_x
execute store result score #r tmp run random value 0..1000
execute if score #r tmp < #a tmp run scoreboard players operation #x tmp = #new_x tmp
execute if score #r tmp < #a tmp run scoreboard players operation #p_o tmp = #p_n tmp

# tellraw @a [{"text":"dx: "},{"score":{"name":"#dx","objective":"tmp"}}]
# tellraw @a [{"text":"p_n: "},{"score":{"name":"#p_n","objective":"tmp"}}]
# tellraw @a [{"text":"p_o: "},{"score":{"name":"#p_o","objective":"tmp"}}]
# tellraw @a [{"text":"a: ","color":"red"},{"score":{"name":"#a","objective":"tmp"}}]
# tellraw @a [{"text":"r: ","color":"red"},{"score":{"name":"#r","objective":"tmp"}}]
# tellraw @a [{"text":"x: ","color":"gold","bold":true},{"score":{"name":"#x","objective":"tmp"}}]
# tellraw @a [{"text":"----------"}]

scoreboard players add #loop tmp 1
execute unless score #loop tmp matches 5.. run function lib:random_normal/loop

5 changes: 5 additions & 0 deletions __global__/data/lib/tags/functions/random_normal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"values": [
"lib:random_normal/interface"
]
}
1,001 changes: 1,001 additions & 0 deletions minigames/wolstreet_wolf.ipynb

Large diffs are not rendered by default.

0 comments on commit dc95a4c

Please sign in to comment.