Convencience function for random + entropy function.

This commit is contained in:
Christoffer Lerno
2023-10-31 22:21:38 +01:00
parent cd7a03c2cf
commit 120e21b80b
4 changed files with 43 additions and 4 deletions

View File

@@ -21,6 +21,11 @@ macro void seed(random, seed)
random.set_seed(@as_char_view(seed));
}
macro void seed_entropy(random)
{
random.set_seed(&&entropy());
}
/**
* @require is_random(random)
**/
@@ -29,6 +34,17 @@ macro int next(random, int max)
return (int)(next_double(random) * max);
}
fn int rand(int max) @builtin
{
tlocal Sfc64Random default_random;
tlocal bool initialized = false;
if (!initialized)
{
seed_entropy(&default_random);
initialized = true;
}
return next(&default_random, max);
}
/**
* @require is_random(random)
**/

View File

@@ -1,4 +1,5 @@
module std::math::random;
import std::hash::fnv32a;
const ODD_PHI64 @local = 0x9e3779b97f4a7c15;
const MUL_MCG64 @local = 0xf1357aea2e62a9c5;
@@ -65,3 +66,27 @@ fn void seeder(char[] input, char[] out_buffer)
out_buffer[..] = ((char*)words.ptr)[:out_chars];
};
}
macro uint hash(value) @local
{
return fnv32a::encode(&&bitcast(value, char[$typeof(value).sizeof]));
}
fn char[8 * 4] entropy()
{
void* addr = malloc(1);
free(addr);
static uint random_int;
random_int += 0xedf19156;
uint[8] entropy_data = {
hash($$TIME),
hash(addr),
hash(&addr),
hash(&entropy),
random_int,
hash(clock::now()),
hash(&DString.init),
hash(mem::heap())
};
return bitcast(entropy_data, char[8 * 4]);
}

View File

@@ -69,10 +69,8 @@ fn void Game.update(Game *game, int guess)
fn void! main()
{
Lcg128Random rand;
random::seed(&rand, clock::now());
int high = 100;
int answer = random::next(&rand, high) + 1;
int answer = rand(high) + 1;
Game game = { .answer = answer, .high = high };
(void)game.play();
io::printfn("Finished in %d guesses.", game.guesses);

View File

@@ -1 +1 @@
#define COMPILER_VERSION "0.4.692"
#define COMPILER_VERSION "0.4.693"