Add Bool:random(p=0.5)

This commit is contained in:
Bruce Hill 2024-08-16 14:18:23 -04:00
parent 0f1c475bb4
commit 04714e00d7
3 changed files with 7 additions and 0 deletions

View File

@ -40,6 +40,11 @@ public Bool_t Bool$from_text(CORD text, bool *success)
}
}
public Bool_t Bool$random(double p)
{
return (drand48() < p);
}
public const TypeInfo $Bool = {
.size=sizeof(bool),
.align=__alignof__(bool),

View File

@ -14,6 +14,7 @@
CORD Bool$as_text(const bool *b, bool colorize, const TypeInfo *type);
bool Bool$from_text(CORD text, bool *success);
Bool_t Bool$random(double p);
extern const TypeInfo $Bool;

View File

@ -85,6 +85,7 @@ env_t *new_compilation_unit(CORD *libname)
{"Memory", Type(MemoryType), "Memory_t", "$Memory", {}},
{"Bool", Type(BoolType), "Bool_t", "$Bool", TypedArray(ns_entry_t,
{"from_text", "Bool$from_text", "func(text:Text, success=!&Bool)->Bool"},
{"random", "Bool$random", "func(p=0.5)->Bool"},
)},
{"Int", Type(IntType, .bits=0), "Int_t", "$Int", TypedArray(ns_entry_t,
{"format", "Int$format", "func(i:Int, digits=0)->Text"},