Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- Pseudo-random number generation enhanced with an external entropy source (webcam image).
- It does not meet the strictest criteria for true randomness, but it comes as close
- as we easily can without specialized hardware. Further more entropy image is
- not updated fast enough to truly be unpredictable within a 1 minute window,
- a better source could be found, and the camera may not be able to deal well with
- night-time, which means entropy may reduce.
- But on a longer timeframe this can not be predicted, nor replicated,
- even though short sequences within it can be individually solved for
- assuming enough data is collected.
- *)
- var entropyUpdateTimer:Double;
- function TrueRandom(x,y: Int32): Int32;
- var
- trueRnd: UInt64;
- rnd: TImage;
- r,t:single;
- data: string;
- begin
- // grab random world data, in our case an image is the source of entropy
- // however this image only updates every 1 minute at the source
- // so that makes 1 minute sequences of this deterministic.
- // For optimal entropy find a live source with *a lot* of data.
- // weather patterns are likely a good source (if can be found online).
- if PerformanceTimer() > entropyUpdateTimer then
- begin
- HTTPClient.GetFile('https://kamera.atlas.vegvesen.no/api/images/0329003_1', 'rndgen.jpg');
- //65 seconds as image updates once every minute.
- entropyUpdateTimer := PerformanceTimer() + 1000*65;
- end;
- // load real world data as simi-undeterministic spice to pseudo random number generation
- rnd := TImage.Create('rndgen.jpg');
- data := HashData(EHashAlgo.SHA256, rnd.data, rnd.Width*rnd.Height*4);
- SetLength(data, 16); // we only care about the first 64 bits.
- rnd.Free();
- //a true-ish random number in the range 0..High(UInt64)
- trueRnd := StrToInt64('$'+data);
- //a true-ish random number in the incomplete range 0..1
- t := trueRnd / High(UInt64);
- r := x + t * 1/t * y * random();
- Result := Round(r);
- end;
- var
- myIds := [543,11,99,664,14];
- begin
- for 0 to 1000 do
- Writeln TrueRandom(0,1000);
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement