mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Convencience function for random + entropy function.
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
Reference in New Issue
Block a user