From 04714e00d781235590553446301ac7e5818b3455 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 16 Aug 2024 14:18:23 -0400 Subject: Add Bool:random(p=0.5) --- builtins/bool.c | 5 +++++ builtins/bool.h | 1 + environment.c | 1 + 3 files changed, 7 insertions(+) diff --git a/builtins/bool.c b/builtins/bool.c index 1e741be1..af2f0ac7 100644 --- a/builtins/bool.c +++ b/builtins/bool.c @@ -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), diff --git a/builtins/bool.h b/builtins/bool.h index 356889d1..716ddd5b 100644 --- a/builtins/bool.h +++ b/builtins/bool.h @@ -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; diff --git a/environment.c b/environment.c index 95cd4673..5fa1b965 100644 --- a/environment.c +++ b/environment.c @@ -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"}, -- cgit v1.2.3