Recent

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

chuckles1066

  • New Member
  • *
  • Posts: 12
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: 2910
  • 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.1.0 r63881 FPC 3.3.1 r40507 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: 1775
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: 7525
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 »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

chuckles1066

  • New Member
  • *
  • Posts: 12
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: 3520
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

  • Sr. Member
  • ****
  • Posts: 302
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: 10446
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.
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

chuckles1066

  • New Member
  • *
  • Posts: 12
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?

GetMem

  • Hero Member
  • *****
  • Posts: 3757
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: 12
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