Skip to content

Commit 906ee93

Browse files
authored
Merge pull request #9 from paweld/gus
own Rng function
2 parents c94bfa7 + fdc0832 commit 906ee93

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

generator/Common/generate.common.pas

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ TGenerator = class(TObject)
2525
procedure BuildStationNames;
2626
function GenerateProgressBar(APBPosition, APBMax, APBWIdth, AFileSize: Int64;
2727
ATimeElapsed: TDateTime): String;
28+
function Rng1brc(Range: longint): longint;
2829
protected
2930
public
3031
constructor Create(AInputFile, AOutputFile: String; ALineCount: Int64);
@@ -158,6 +159,20 @@ function TGenerator.GenerateProgressBar(APBPosition, APBMax, APBWIdth, AFileSize
158159
ATimeElapsed)]);
159160
end;
160161

162+
function TGenerator.Rng1brc(Range: longint): longint;
163+
const
164+
state: Array [0..1] of DWord = (46668267, 7266);
165+
var
166+
s0, s1, s2: DWord;
167+
begin
168+
s0 := state[0];
169+
s1 := state[1] xor s0;
170+
s2 := RolDWord(s1 * 3, 5) * 7;
171+
Result := longint(Int64(s2 * range) shr 32);
172+
state[0] := s2;
173+
state[1] := s0 xor (s1 shl 9);
174+
end;
175+
161176
procedure TGenerator.Generate;
162177
var
163178
index, progressCount, progressBatch: Int64;
@@ -232,10 +247,8 @@ procedure TGenerator.Generate;
232247
// Generate the file
233248
for index := 1 to FLineCount do
234249
begin
235-
stationId := Random(stationsCount);
236-
// This is all paweld magic:
237-
// From here
238-
randomTemp := Random(temperaturesCount);
250+
stationId := Rng1brc(stationsCount);
251+
randomTemp := Rng1brc(temperaturesCount);
239252
Move(stationArray[stationId][1], chunkLine[chunkLen + 1],
240253
LenStationArray[stationId]);
241254
Inc(chunkLen, LenStationArray[stationId]);

0 commit comments

Comments
 (0)