Recent

Author Topic: Find out which seed randomize has used?  (Read 2295 times)

chuckles1066

  • New Member
  • *
  • Posts: 20
Find out which seed randomize has used?
« on: July 04, 2020, 04:41:10 pm »
Hello,

I have the following bit of code which I am compiling into a dll.
Code: Pascal  [Select][+][-]
  1. function Dice: Integer; {$ifdef windows}stdcall{$else}cdecl{$endif};
  2. var
  3.   D1, D2: Integer;
  4. begin
  5.   // call randomize once and never again.
  6.   if not RandomizeAlreadyCalled then begin
  7.     Randomize;
  8.     RandomizeAlreadyCalled := True;
  9.   end;
  10.  
  11.   D1 := Random(6) + 1;
  12.   D2 := Random(6) + 1;
  13.  
  14.   Result := D1 * 8 + D2;
  15. //Export the dice function by name
  16. exports Dice;
  17.  

Is there a way I can get a message box or a form to pop up telling me what seed (randseed?) value has been used when I've called randomize?

Many thanks.

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Find out which seed randomize has used?
« Reply #1 on: July 04, 2020, 06:05:06 pm »
Code: Pascal  [Select][+][-]
  1.   // call randomize once and never again.
  2.   if not RandomizeAlreadyCalled then begin
  3.     Randomize;
  4.     ShowMessage(inttostr(RandSeed));
  5.     RandomizeAlreadyCalled := True;
  6.   end;
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/

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Find out which seed randomize has used?
« Reply #2 on: July 04, 2020, 06:54:59 pm »
Hi!

You can set your RandomSeed by code:

Code: Pascal  [Select][+][-]
  1. Randseed := 19610412; // First man in space: Juri Gagarin

Randseed is a DWord.

Winni



wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Find out which seed randomize has used?
« Reply #3 on: July 04, 2020, 06:57:48 pm »
Not your question, but I think it is important to know that by setting RandSeed to a given number instead of calling Randomize you can get random numbers which are reproducible from run to run. This is extremely important when you have to debug an error which is related to some specific numbers. Of course this trick should be used only during debugging.

[EDIT]
Winni was faster...
« Last Edit: July 04, 2020, 06:59:30 pm by wp »

chuckles1066

  • New Member
  • *
  • Posts: 20
Re: Find out which seed randomize has used?
« Reply #4 on: July 05, 2020, 09:53:01 am »
Thanks  to all, much appreciated....very useful to know that you can "hard code" the seed and be able to reproduce the exact sequence time and time again.....definitely good for debugging.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Find out which seed randomize has used?
« Reply #5 on: July 05, 2020, 02:17:32 pm »
Hi!

You can set your RandomSeed by code:

Code: Pascal  [Select][+][-]
  1. Randseed := 19610412; // First man in space: Juri Gagarin

Randseed is a DWord.

Winni

Well, according to our present history that is  :D
The only true wisdom is knowing you know nothing

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Find out which seed randomize has used?
« Reply #6 on: July 05, 2020, 09:10:09 pm »
Of course this trick should be used only during debugging.
Or when you are doing some sort of (scientific) experiment where you publish the results and your result should be exactly reproducable (like a simulation or so, where you want the same events to occur but handle them differently to be comparable)

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Find out which seed randomize has used?
« Reply #7 on: July 05, 2020, 09:43:42 pm »
Indeed reproducability on a given dataset is really important for statistics in general.. Nothing to do with debugging.
Specialize a type, not a var.

chuckles1066

  • New Member
  • *
  • Posts: 20
Re: Find out which seed randomize has used?
« Reply #8 on: July 06, 2020, 11:01:45 am »
Which has given me an idea......is it possible to inject a number into my dll (first post)?

So, have a pop up (or, preferably a form)  that says "what seed do you want?" and the user either writes in a random value like 12345678 or if he leaves it blank the program chooses one?

balazsszekely

  • Guest
Re: Find out which seed randomize has used?
« Reply #9 on: July 06, 2020, 01:06:06 pm »
@chuckles1066

See attached projects:
1. Compile dicelibrary.lpi first
2. Copy the dll/so inside the test folder
3. Run the test project

chuckles1066

  • New Member
  • *
  • Posts: 20
Re: Find out which seed randomize has used?
« Reply #10 on: July 08, 2020, 09:56:03 am »
Apologies for the delay in responding, thank you sooooo much for your effort and help, it is truly appreciated.

Some of you guys have so much knowledge, I can only aspire to one day be able to give something back to the forum.

 

TinyPortal © 2005-2018