สิ่งที่เป็นปัญหาที่ดีกว่าสำหรับ PCG.SE กว่าที่จะใช้PCG, ที่ดีกว่าการสร้างตัวเลขสุ่ม ? กระดาษใหม่นี้อ้างว่าจะนำเสนอตัวสร้างตัวเลขสุ่มแบบสุ่มที่รวดเร็วและคาดเดายาก
การใช้ C น้อยที่สุดนั้นมีอยู่แค่เก้าบรรทัด
// *Really* minimal PCG32 code / (c) 2014 M.E. O'Neill / pcg-random.org
// Licensed under Apache License 2.0 (NO WARRANTY, etc. see website)
typedef struct { uint64_t state; uint64_t inc; } pcg32_random_t;
uint32_t pcg32_random_r(pcg32_random_t* rng)
{
uint64_t oldstate = rng->state;
// Advance internal state
rng->state = oldstate * 6364136223846793005ULL + (rng->inc|1);
// Calculate output function (XSH RR), uses old state for max ILP
uint32_t xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
uint32_t rot = oldstate >> 59u;
return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
}
(จาก: http://www.pcg-random.org/download.html )
คำถามคือคุณสามารถทำได้ดีกว่า
กฎระเบียบ
เขียนโปรแกรมหรือกำหนดฟังก์ชั่นที่ใช้ PCG กับเลขจำนวนเต็ม 32 บิตที่ไม่ได้ลงชื่อ สิ่งนี้ค่อนข้างกว้าง: คุณสามารถพิมพ์ลำดับอนันต์กำหนดpcg32_random_r
ฟังก์ชันและโครงสร้างที่สอดคล้องกัน ฯลฯ
คุณต้องสามารถสร้างตัวสร้างตัวเลขสุ่มของคุณเท่ากับฟังก์ชัน C ต่อไปนี้:
// pcg32_srandom(initstate, initseq)
// pcg32_srandom_r(rng, initstate, initseq):
// Seed the rng. Specified in two parts, state initializer and a
// sequence selection constant (a.k.a. stream id)
void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initstate, uint64_t initseq)
{
rng->state = 0U;
rng->inc = (initseq << 1u) | 1u;
pcg32_random_r(rng);
rng->state += initstate;
pcg32_random_r(rng);
}
(จากpcg_basic.c:37
:)
การเรียกตัวสร้างตัวเลขสุ่มโดยไม่ต้องทำการเพาะมันเป็นพฤติกรรมที่ไม่ได้กำหนด
หากต้องการตรวจสอบการส่งของคุณได้อย่างง่ายดายตรวจสอบว่าเมื่อ seeded ด้วยinitstate = 42
และinitseq = 52
ผลลัพธ์คือ2380307335
:
$ tail -n 8 pcg.c
int main()
{
pcg32_random_t pcg;
pcg32_srandom_r(&pcg, 42u, 52u);
printf("%u\n", pcg32_random_r(&pcg));
return 0;
}
$ gcc pcg.c
$ ./a.out
2380307335
เกณฑ์การให้คะแนน
คะแนนมาตรฐาน วัดเป็นไบต์ ต่ำสุดดีที่สุด ในกรณีที่เสมอกันการส่งก่อนหน้านี้จะชนะ ช่องโหว่มาตรฐานใช้
ตัวอย่างสารละลาย
คอมไพล์ภายใต้gcc -W -Wall
หมดจด (เวอร์ชั่น 4.8.2)
เปรียบเทียบการส่งของคุณกับสิ่งนี้เพื่อให้แน่ใจว่าคุณได้รับลำดับเดียวกัน
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
typedef struct { uint64_t state; uint64_t inc; } pcg32_random_t;
uint32_t pcg32_random_r(pcg32_random_t* rng)
{
uint64_t oldstate = rng->state;
// Advance internal state
rng->state = oldstate * 6364136223846793005ULL + (rng->inc|1);
// Calculate output function (XSH RR), uses old state for max ILP
uint32_t xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
uint32_t rot = oldstate >> 59u;
return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
}
void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initstate, uint64_t initseq)
{
rng->state = 0U;
rng->inc = (initseq << 1u) | 1u;
pcg32_random_r(rng);
rng->state += initstate;
pcg32_random_r(rng);
}
int main()
{
size_t i;
pcg32_random_t pcg;
pcg32_srandom_r(&pcg, 42u, 52u);
for (i = 0; i < 16; i++)
{
printf("%u\n", pcg32_random_r(&pcg));
}
return 0;
}
ลำดับ:
2380307335
948027835
187788573
3952545547
2315139320
3279422313
2401519167
2248674523
3148099331
3383824018
2720691756
2668542805
2457340157
3945009091
1614694131
4292140870