Call to Randomize when opening an xlsx spreadsheet

Hi folks, in chasing down a problem I discovered that there is a call to Randomize in TXlsxChartPlotArea.Create. I'm sure it is there for a good reason, but it was unexpected, and messed up something I was doing with random numbers. Is there any way to know that this might happen, or do I need to preserve the value of RandSeed every time I open an Excel file?

The thing is, for some stuff we need some "real" random numbers. Or if not real, at least not as bad as random without randomize. The most obvious example is if you have the formula "=Random()" in a cell, but there are others as you've find out.

But I see the problem, in fact, for our own tests those calls are problematic as they cause different results every time we run the tests. (And that's another reason we try to keep them to a minimum). The source code we ship is stripped of debug code, but the actual lines in that call to randomize are:

{$IFDEF UNITTESTING}
  Rnd := GetRandom;
{$ELSE}
  Randomize;
{$ENDIF}

I am not sure on what is the best solution we can offer here, but this is what I can think:

  1. We could implement our own random generator and leave Delphi's alone. It is an option, I am not sure how much effort it could be, but FlexCel already reimplements a lot of built-in functionality, so one spot more to the leopard won't change much.
  2. We could give you a property like "DoNotRandomize" and if that property is true, we don't call Randomize. But the problem with this approach is that you are unlikely to find that property between all the ones already there, and even myself I will forget about it in a year or two, so when you have the problem again, neither you or me will likely remember about the property
  3. We could try to preserve the seed, but that's not trivial, since there are too many entry points from where the seed can be modified.
  4. We could require you to restore the seed as you mention. But same as 2, nobody will remember later about this, so it is not really a solution.

I don't particularly like any of the options, but I'll think about what can be the best and try to implement that.

just to keep you updated, we finally went with option 1, using xorshiro ( https://prng.di.unimi.it ). It was really simple and it is even a better random generator than the built-in inside delphi. So no more calls to randomize in the next release, scheduled sometime the week past the next one.

Thanks Adrian, that's superb service. I will look forward to the new version.

Hi,

We've just released FlexCel 7.20, it should have this fixed. No more Randomize calls :slight_smile:

1 Like

Wonderful, thank you!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.