Use 'pcg64_fast' for RNG
All checks were successful
ci/woodpecker/push/check Pipeline was successful

This is a higher quality source of randomness than a XOR-shift
generator, while still being fast and easy to write.
This commit is contained in:
Bruno BELANYI 2024-04-06 19:30:53 +01:00
parent 7dd0da6628
commit 93d255623b
2 changed files with 144 additions and 140 deletions

View file

@ -60,138 +60,138 @@ impl MagicMoves {
// region:sourcegen // region:sourcegen
/// A set of magic numbers for bishop move generation. /// A set of magic numbers for bishop move generation.
pub(crate) const BISHOP_SEED: [u64; Square::NUM_VARIANTS] = [ pub(crate) const BISHOP_SEED: [u64; Square::NUM_VARIANTS] = [
4908958787341189172, 4634226011293351952,
1157496606860279808, 6918109887683821586,
289395876198088778, 76562328660738184,
649648646467355137, 7242919606867744800,
19162426089930848, 13871652069997347969,
564067194896448, 1171657252671901696,
18586170375029026, 147001475087730752,
9185354800693760, 1752045392763101248,
72172012436987968, 288406435526639744,
317226351607872, 4612213818402029888,
2597178509285688384, 9808848818951710728,
1162205282238464, 9223394181731320840,
144154788211329152, 54047645651435648,
172197832046936160, 9224780030482579712,
4625762105940000802, 9049059098626048,
1477217245166903296, 1442330840700035221,
2251937789583872, 1126037887157508,
289373902621379585, 1153488887004529665,
4616200855845409024, 290485130928332936,
2251909637357568, 9226749771011592258,
3532510975437640064, 148636405693678112,
563517968228352, 2260596997758984,
562953309660434, 73470481646424336,
1196005458310201856, 2341907012146823680,
2350914225914520576, 2314955761652335121,
2287018679861376, 2265544246165632,
13836188353273790593, 13598764778463296,
11267795163676832, 563087425962496,
297519119119499264, 563087425962048,
18588344158519552, 2163991853573081088,
10453428171813953792, 567353402270020,
72128237668534272, 6488844433713538048,
1298164929055953920, 288810987011448834,
865575144395900952, 11830884701569344,
9293076573325312, 2747549955031826688,
108104018148197376, 35734665298432,
578503662094123152, 18025943920672800,
4665870505495102224, 292892945404789012,
6066493872259301520, 1153520472160470528,
285877477613857, 2260949167801860,
2328941618281318466, 155446765112299521,
721165292771739652, 379008324189818944,
4899973577790523400, 4616480181217005576,
75050392749184, 576461027453960704,
2305878200632215680, 2450556349601564416,
11530099074925593616, 1160556519943569536,
290561512873919880, 4612900059821375552,
18652187227888000, 5477089643453251617,
3379933716168704, 9223532084785594632,
9223409493537718272, 2810391870219355200,
22273835729926, 36594222015453185,
1152921524003672064, 4612011546951352320,
4647812741240848385, 2392883590201344,
1244225087719112712, 1152956706186200064,
7367907171013001728, 9009415592510464,
9263922034316951570, 81077999302148128,
300758214358598160, 576746627483043968,
4611686331973636096, 301267327789056,
2377900605806479360, 39586720976896,
6958097192913601024, 720878306081243648,
864691130877743617, 9223512777841312257,
703824948904066, 5764609859566698625,
612700674899317536, 8088544233436348496,
180742128018784384, 4612856276794474560,
]; ];
/// A set of magic numbers for rook move generation. /// A set of magic numbers for rook move generation.
pub(crate) const ROOK_SEED: [u64; Square::NUM_VARIANTS] = [ pub(crate) const ROOK_SEED: [u64; Square::NUM_VARIANTS] = [
2341871943948451840, 180144122814791812,
18015635528220736, 10448386594766422036,
72066665545773824, 9403533616331358856,
1188959097794342912, 108095189301858304,
12141713393631625314, 72076290316044288,
720649693658353672, 36066182562054145,
36029896538981888, 4647717564258980096,
36033359356363520, 13979173385364603396,
140746619355268, 4620833992751489152,
1158339898446446661, 297800804633419904,
36591886560003650, 578009002156298240,
578853633228023808, 2450099003505838082,
2392554490300416, 1175721046778052864,
140814806160384, 20406952999780864,
180706952366596608, 1175861788231598592,
10696087878779396, 36169538802827392,
1153260703948210820, 288371663414771712,
310748649170673678, 423313050501155,
36311372044308544, 604731668136450,
9223444604757615104, 580261214513399808,
1267187285230592, 297661437206136832,
282574622818306, 1750211954976489600,
18722484274726152, 9020393411186696,
2271591090110593, 9259543770406356001,
1153063519847989248, 44532368556032,
10168327557107712, 10376381507760693256,
4507998211276833, 52778707714176,
1153203035420233728, 4612829512676149248,
4631961017139660032, 1882513444629184528,
2454499182462107776, 2369460754144428160,
289367288355753288, 9223380850137104901,
18015815850820609, 2666413562481640036,
9268726066908758912, 141012643087392,
11547264697673728000, 16735517094631719424,
2314929519368081536, 17594358702087,
140943655192577, 2344264412262574084,
20266215511427202, 422813768878080,
180706969441535248, 1126450811896320,
1302683805944911874, 54466576291772936,
11534000122299940994, 42784758060548372,
22676602724843520, 292874851780165648,
4639271120198041668, 18015364885839937,
1302104069046927376, 282644818493504,
9184220895313928, 1184447393488764944,
4612249105954373649, 4649966632473477184,
562984581726212, 563499910594566,
2312678200579457040, 17632049496086,
4647736876550193157, 18502729728001,
3170604524138139776, 140742121013504,
4684447574787096704, 9711024139665536,
20283792725901696, 246293205270784,
1152992019380963840, 290772515771392256,
117383863558471808, 9230131836490350720,
1153488854922068096, 73326432604127360,
17596884583424, 453174886517643776,
90074759127192064, 2396271245728563712,
4900502436426416706, 324259242966026501,
4573968656793901, 288953994406543363,
1161084564408385, 1153557061259362338,
1657887889314811910, 40533496293515441,
4614501455660058690, 1407392197644307,
4612530729109422081, 1729945211427624002,
642458506527236, 587808330812164100,
1116704154754, 9511606812128903298,
]; ];
// endregion:sourcegen // endregion:sourcegen

View file

@ -3,21 +3,25 @@ pub trait RandGen {
fn gen(&mut self) -> u64; fn gen(&mut self) -> u64;
} }
// A simple XOR-shift RNG implementation, for code-generation. // A simple pcg64_fast RNG implementation, for code-generation.
#[cfg(test)] #[cfg(test)]
pub struct SimpleRng(u64); pub struct SimpleRng(u128);
#[cfg(test)] #[cfg(test)]
impl SimpleRng { impl SimpleRng {
pub fn new() -> Self { pub fn new() -> Self {
Self(4) // https://xkcd.com/221/ Self(0xcafef00dd15ea5e5 | 1) // https://xkcd.com/221/
} }
pub fn gen(&mut self) -> u64 { pub fn gen(&mut self) -> u64 {
self.0 ^= self.0 >> 12; const MULTIPLIER: u128 = 0x2360_ED05_1FC6_5DA4_4385_DF64_9FCC_F645;
self.0 ^= self.0 << 25; const XSHIFT: u32 = 64; // (128 - 64 + 64) / 2
self.0 ^= self.0 >> 27; const ROTATE: u32 = 122; // 128 - 6
self.0
self.0 = self.0.wrapping_mul(MULTIPLIER);
let rot = (self.0 >> ROTATE) as u32;
let xsl = (self.0 >> XSHIFT) as u64 ^ (self.0 as u64);
xsl.rotate_right(rot)
} }
} }
@ -36,10 +40,10 @@ mod test {
fn rng() { fn rng() {
let mut rng = SimpleRng::new(); let mut rng = SimpleRng::new();
assert_eq!(rng.gen(), 134217733); assert_eq!(rng.gen(), 64934999470316615);
assert_eq!(rng.gen(), 4504699139039237); assert_eq!(rng.gen(), 15459456780870779090);
assert_eq!(rng.gen(), 13512173405898766); assert_eq!(rng.gen(), 13715484424881807779);
assert_eq!(rng.gen(), 9225626310854853124); assert_eq!(rng.gen(), 17718572936700675021);
assert_eq!(rng.gen(), 29836777971867270); assert_eq!(rng.gen(), 14587996314750246637);
} }
} }