h3rald / min

A small but practical concatenative programming language and shell
https://min-lang.org
MIT License
309 stars 23 forks source link

What's a good way to re-seed `random`? #23

Closed mwgkgk closed 6 years ago

mwgkgk commented 6 years ago

To replicate, run min -i:

5 random
{1} -> 2
5 random
{2} -> 4
5 random
{3} -> 3
5 random
{4} -> 1

Close interpreter, restart, repeat, observe same sequence.

How to re-seed the random? I'm trying to shuffle a list of tests so that they run independently of each other, just in case some tests might affect others.

Thank you :1st_place_medal:

mwgkgk commented 6 years ago

GNU Coreutil called shuf takes care of the shuffling!

; Run a list of tests in a randomized order
; (test1 test2 ... testN)
( quote-map =tests
  ; Construct a list of indices
  "shuf -i 0-$1" (tests size 1 -) => % & /output strip "\n" split (int) map
  ; Use each index to get and run the corresponding test
  (:i tests i get apply) map
  ; Avoid using pop for cleanup
  harvest =st
  (st () ==) ("\nContaminated stack:\n$1" (st string) => % puts!) unless
) :randomize-tests

If re-seeding min's random is too far fetched to be actionable, please close this. If the code above looks like it could use improvement please let me know. :+1:

h3rald commented 6 years ago

Ohhh nasty! Sure, I think it could be done simply by exposing Nim's randomize proc exposed by the random module...

I'll have a look this weekend if I get a chance -- I need to make min properly compatible with Nim 0.18.0 anyway.