Lazarus

Free Pascal => Beginners => Topic started by: chuckles1066 on July 04, 2020, 04:41:10 pm

Title: Find out which seed randomize has used?
Post by: chuckles1066 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.
Title: Re: Find out which seed randomize has used?
Post by: Blaazen 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;
Title: Re: Find out which seed randomize has used?
Post by: winni 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


Title: Re: Find out which seed randomize has used?
Post by: wp 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...
Title: Re: Find out which seed randomize has used?
Post by: chuckles1066 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.
Title: Re: Find out which seed randomize has used?
Post by: jamie 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
Title: Re: Find out which seed randomize has used?
Post by: Warfley 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)
Title: Re: Find out which seed randomize has used?
Post by: Thaddy 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.
Title: Re: Find out which seed randomize has used?
Post by: chuckles1066 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?
Title: Re: Find out which seed randomize has used?
Post by: GetMem 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
Title: Re: Find out which seed randomize has used?
Post by: chuckles1066 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