Recent

Author Topic: Will random function produce the same sequence on all platforms for agiven seed?  (Read 12745 times)

Wodzu

  • Full Member
  • ***
  • Posts: 171
Hi.

The tille says it all.
I want to obtain exactly the same sequence of numbers based on the RandSeed regardless of the platform.
Is current implementation of Random Generator works that way? (I can not check this out by myself).

Also, I would like to port Random Generator for Delphi. Would that be a problem?

I am writing a random terrain generator and I must be able to generate the same terrain from Delphi and FPC.

thanks.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
I tested this code:
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
begin
  randseed:=100;
  caption:=inttostr(random(100));
end;
Lazarus win32 returns 54, and Delphi7 13. => Random function differs. However i'm pretty sure that the random algorithm is same when you use Lazarus on any platform.

Wodzu

  • Full Member
  • ***
  • Posts: 171
Thanks, I've test it too on Delphi and FPC.

I need some generator which could be setup on both compilers and produce the same results... :(

Elmug

  • Hero Member
  • *****
  • Posts: 849
Wodzu,

Regardless of platform, random functions would produce different results, that's why they are called 'random'.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Quote
I am writing a random terrain generator and I must be able to generate the same terrain from Delphi and FPC.

You need to implement some pseudo-random generator yourself.

http://www.math.utah.edu/~pa/Random/Random.html
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Troodon

  • Sr. Member
  • ****
  • Posts: 484
I need some [random] generator which could... produce the same results...

That's a contradiction in terms...
Lazarus/FPC on Linux

ezlage

  • Guest
If really is pseudo-random, you can find a relation and make a mathematical expression that define the variation from each plataform and applying it in your code you will transform the result in the same for all plataforms.

Sorry by my poor english...
I hope that helps.

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 729
Blaazen and ezlage are correct.  I don't know of a fully documented cross platform random number generator.

If memory serves, you'll also find pascal source for a pseudorandom number generator (and they're ALL psuedorandom, with distinctive statistical profiles) in the famous book Numerical Recipes.  Pascal for all of that book's algorithms are freely available on the internet from the book authors.

avra

  • Hero Member
  • *****
  • Posts: 2584
    • Additional info
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Why don't you just copy random() function behaviour from Lazarus systemh.inc and system.inc. Deep down it goes to
Code: [Select]
function genrand_MT19937: longint;which doesn't seem overly long. I don't see anything about the code that would compile differently on different platforms, apart from 2 compiler directives which i don't know when they're active:
Code: [Select]
{$ifdef FPC_HAS_FEATURE_RANDOM}
{$ifndef FPUNONE}
But if you insist of using just the basic code, wouldn't it work the same?

So can someone say what my earlier mentioned code in formCreate would result on linux, win64, mac or something?

Shebuka

  • Sr. Member
  • ****
  • Posts: 429
So can someone say what my earlier mentioned code in formCreate would result on linux, win64, mac or something?
Mac OS X 10.6, Lazarus SVN, fpc 2.4.4, both Carbon/Qt -----> 54

Wodzu

  • Full Member
  • ***
  • Posts: 171
I need some [random] generator which could... produce the same results...

That's a contradiction in terms...

Troodon, that is something I haven't said. Why do you modify my words and then quote it.

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Troodon, that is something I haven't said. Why do you modify my words and then quote it.

Given the title of this thread that's how I read your posting, my apologies if I misunderstood what you meant.
« Last Edit: September 09, 2011, 05:06:30 pm by Troodon »
Lazarus/FPC on Linux

Wodzu

  • Full Member
  • ***
  • Posts: 171
Troodon no harm feelings ;D

Thanks guys for the tip with the book, looks really interesting for future usage.

@User137 I was thinking about that but I guess I try to implement someihtng on my own. Maybe a Mersenne Twister: http://en.wikipedia.org/wiki/Mersenne_Twister

Regards.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12712
  • FPC developer.

@User137 I was thinking about that but I guess I try to implement someihtng on my own. Maybe a Mersenne Twister: http://en.wikipedia.org/wiki/Mersenne_Twister

Afaik the FPC one already is. I agree with User137 that stuff the FPC random generator in an unit and stuffing it in an unit is the smart thing to do.

This is the only way to force always the same sequence (between fpc and delphi, but also between fpc and future versions of fpc)
 

 

TinyPortal © 2005-2018