-
Notifications
You must be signed in to change notification settings - Fork 184
Description
Hey guys,
I run in the problem that using waves.randPreDefined = 0; sets the random generator seed to 0, which effectively defines a deterministic sequence of numbers drawn from the default random number generator algorithm. This deterministic sequence will repeat itself in any instance of Matlab, when re-started, or when the random number generator is called again with the same seed number ( >> rng(0) )
I think the description in the manual and the annotations in waveClass.m are bit misleading:
Note that, by default, the random wave phase for irregular waves are generated arbitrarily (waves.randPreDefined=0). If the user species waves.randPreDefined=1 in the input file, the random phase of the waves will be generated using the rand function in MATLAB with a seed value of 1. This gives the user an option to generate the same random wave every time if needed.
As far as I can see, either of the options (waves.randPreDefined = 0; or waves.randPreDefined = 1;) will give a deterministic sequence of numbers from the random number generator. One with a seed number equal to 0 (because it uses directly rand, which will use rng with default values) and the other equal to 1. Every time you call the random number generator >> rng( seed_number ) it will start the same deterministic sequence corresponding to the given seed_number.
Ok, this is getting too long, but I hope it is clear... So, when rand is called for the first time it starts the random number generator, succesive calls to rand will contine drawing numbers from the sequence defined by the seed, until rng is called again to reset the seed number.
The problem then is that running batch simulations on different instances of Matlab with waves.randPreDefined = 0; will output simulations on each instance with the same sequence of random phase. Because the first simulation of each batch, in each Matlab instance, starts rng with the same seed.
The solution is to give a different seed_number (i.e., rng(seed_number) or waves.randPreDefined = seed_number;) for each batch. The easiest way to do this is to use rng('shuffle).
I implemented this in my repository (see this commit). I meant to make a pull request, but there are many changes that are prbably not relevant for the oficial WecSim repository and since I did not had a welll organized branching method, I think it will be a pain to pull it if you wanted.
Anyhow, it is trivial to implement, but I personally think you can save people time if you add the shuffle option because it guarantees a sort of random initialization of rng when rand is used.