Last active
June 5, 2019 09:52
-
-
Save nedzadarek/736f6188d72d6011d25e7292eff98460 to your computer and use it in GitHub Desktop.
Generating chance (win/fail) based on % (float) - after each 100 tries it increases it's chances.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red[ | |
author: {Nędza Darek} | |
version: 0.0.2 | |
license: { | |
- point to this gist/github | |
- no warranties | |
- use/modify everywhere | |
} | |
] | |
float-incr: function [v] [ | |
digits-after-dot: length? find/tail (to-string v) #"." | |
s: copy "0." | |
loop digits-after-dot - 1 [ | |
append s #"0" | |
] | |
append s #"1" | |
v + to-float s | |
] | |
random-loot: function [chance [float!]][ | |
if chance > 1.0 [do make error! "chance bigger than 1.0"] | |
digits-after-dot: (length? to-string chance) - 2 ; "0." | |
chances: make block! N: 10 ** digits-after-dot | |
chance-win: to-integer at (to-string chance) 3 | |
chance-fail: N - chance-win | |
; thank you @rebolek for reminding me about loop | |
loop chance-fail[ | |
append chances 'fail | |
] | |
loop chance-win[ | |
append chances 'win | |
] | |
random chances | |
random/only chances ; pick one | |
] | |
state: 'fail | |
chance: 0.001 | |
number-of-tries: 0 | |
while [state = 'fail][ | |
number-of-tries: number-of-tries + 1 | |
state: random-loot chance | |
if 0 = mod number-of-tries 100 [ chance: float-incr chance] | |
] | |
number-of-tries |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment