Results 1 to 10 of 20

Thread: Procedural world generation - ground and traps seems to not generating.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    So why following program
    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.
    gives me this output:
    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.

  2. #2
    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.

  3. #3
    Quote Originally Posted by Darkhog View Post
    So why following program
    ...
    gives me this output:
    16
    2
    16
    29
    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
    Code:
    random := int64((qword(cardinal(genrand_MT19937)) or ((qword(cardinal(genrand_MT19937)) shl 32))) and $7fffffffffffffff);
    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.
    Last edited by User137; 07-06-2013 at 08:11 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •