So this code:
will behave same asCode:Randseed:= 3; Num:=Random(30); Num:=Random(30); Num:=Random(30);
right?Code:Num:=Random2(30; 3, 2);
So this code:
will behave same asCode:Randseed:= 3; Num:=Random(30); Num:=Random(30); Num:=Random(30);
right?Code:Num:=Random2(30; 3, 2);
No, Num:=Random2(30, 3, 2); behaves like:
If you doCode:oldSeed:=randseed randseed:=3+2; Num:=Random(30); randseed:=oldSeed;
Then Num1 = Num2. But if you change offsetCode:Num1:=Random2(30, 3, 0); Num2:=Random2(30, 3, 0);
Then Num1 and Num2 will differ. But you can always replicate those numbers again with same parameters.Code:Num1:=Random2(30, 3, 0); Num2:=Random2(30, 3, 1);
Last edited by User137; 03-06-2013 at 11:51 AM.
So why following program
gives me this output:Code:program Project1; {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Classes, Crt; { you can add units after this } var num:Integer; function Random2(const seed, offset: cardinal): double; overload; var oldSeed: cardinal; begin oldSeed:=randseed; // Save old seed randseed:=seed+offset; // Shouldn't need to check high(cardinal) ranges. // Overflowing should still do the math perfectly. result:=random(); randseed:=oldSeed; // Restore it afterwards, // So that use of normal Random() outside of this function is not interrupted. end; function Random2(const n: cardinal; const seed, offset: cardinal): double; overload; begin result:=trunc(Random2(seed, offset)*n); end; begin RandSeed:=3; num:=Random(30); writeln(num); num:=Random(30); writeln(num); num:=Trunc(Random2(30,3,0)); writeln(num); num:=Trunc(Random2(30,3,1)); writeln(num); readkey; end.
16
2
16
29
?
I need random function which will pass test, e.g. with offset of 1 it'll generate same value like second random.
Anyway, I've made it working. Instead of putting circles at random, I've used worm algorithm (dunno if it is known one, I've made it on my own). Basically I'm carving map like worm passing through ground would do.
There's only 1 explanation that comes to mind, that normal Random() uses other internal variables for the result, not just seed.
I also changed the second function result to cardinal, only the first one was designed to return double. See that it uses Trunc in it, then you used it second time afterwards, which i didn't intend. But this doesn't change the outcome, such thing would seem to need a custom made random algorithm.
edit: No, it's just seed but it can advance the randseed one or 2 times on one call
genrand_MT19937()-function is used twice in the sentence there. Seems to be well over 99.999% chance for it to call it twice, because chances for first genrand returning 0 is slim.Code:random := int64((qword(cardinal(genrand_MT19937)) or ((qword(cardinal(genrand_MT19937)) shl 32))) and $7fffffffffffffff);
Last edited by User137; 07-06-2013 at 08:11 PM.
Bookmarks