We have an exciting announcement about badges coming in May 2025. Until then, we will temporarily stop issuing new badges for course completions and certifications. However, all completions will be recorded and fulfilled after May 2025.
Photonics

Photonics

Topics related to Lumerical and more.

`findmodes` resets the random number generator

TAGGED: , ,

    • amitrotem2
      Subscriber
      calling `findmodes` resets the random number generator, meaing that calling `findmodes; rand();` multiple times gives the same output.
      running on 2023 R2.3
      example to reproduce the bug;
      ```
      clc;
      cleardcard;
      closeall;
      newproject;
      ## design
      addrect;
      set("y",0);set("y span",1e-6);
      set("x",0);set("x span",1e-6);
      set("z",0);set("z span",0.2e-6);
      set("material", "Si (Silicon) - Palik");
      ## solver
      addfde;
      set("solver type", "2D X normal");
      set("x",0);
      set("y",0);set("y span",2e-6);
      set("z",0);set("z span",1e-6);
      set("number of trial modes", 4);
      set("mesh cells z", 50);
      set("mesh cells y", 50);
      ## bug;
      findmodes;
      ?rand(); # same number
      findmodes;
      ?rand(); # same number
      findmodes;
      ?rand(); # same number
      findmodes;
      ?rand(); # same number
      ```
    • Guilin Sun
      Ansys Employee

      findmodes its self uses random seeds to calculate the modes, no need to use "rand" again.

      "rand" its self needs a seed. In order to set it totally random, please try this 

      randreset

      https://optics.ansys.com/hc/en-us/articles/360034406234-randreset-Script-command

      findmodes;

      ?rand(); # same number

      findmodes;

      randreset(1);

      ?rand(); # same number

      findmodes;

      randreset(3);

      ?rand(); # same number

      findmodes;

      randreset(5);

      ?rand(); # same number

       

      Otherwise  since its seed is the same so the result is the same.

       

    • amitrotem2
      Subscriber

      What you said makes no sense to me.

      The problem is that calling findmodes is like calling randreset

      This makes no sense 

    • Guilin Sun
      Ansys Employee

       

      not sure we are talking the same issue. Your original question says “

       

      calling findmodes; rand(); multiple times gives the same output.
      running on 2023 R2.3"

      It is true:

      findmodes;
      ?rand(); # same number
      findmodes;
      ?rand(); # same number
      findmodes;
      ?rand(); # same number
      findmodes;
      ?rand(); # same number
      result: 
      0.449832  
      result: 
      0.449832  
      result: 
      0.449832  
      result: 
      0.449832  
      and give different seeds for rand it gives different result as expected:
      findmodes;
      ?rand(); # same number
      findmodes;
      randreset(1);
      ?rand(); # same number
      findmodes;
      randreset(3);
      ?rand(); # same number
      findmodes;
      randreset(5);
      ?rand(); # same number
      result: 
      0.449832  
      result: 
      0.417022  
      result: 
      0.550798  
      result: 
      0.221993  



       

      I got what I mean. Please clarify your purpose: do you want to get the same result or different result?

       

    • amitrotem2
      Subscriber

      I don't want a workaround, I want the developers to fix the bug.

      Calling rand multiple times gives different results, as should be.

      Why does calling findmodes reset the stream? This is a bug.

    • Guilin Sun
      Ansys Employee

       

      Sorry, it is not a bug, it is desired result. Internaly rand picks up the result of findmodes. It is a fixed number so rand has exactly the same seed.

      I have provided you a method to give random seeds. If you do not want the workaround, there is no other way to make it random.

      you can simply add one more line of script:

      randreset.

       

      For example:

      for(i=1:5){

      findmodes;

      randreset;

      ?rand(); # same number

      }

      It will give different rand result  every time, every run.

      With given reset seed is to have duplicable result. This is desired as in the random simulation, people complained that their result was different from online example and everytime they run the result is different. So we fixed this. as long as the random seed is the same the rand will give the same result.

       

       

    • amitrotem2
      Subscriber

      Nowhere in the documentation does it mention that findmodes affect rand.

      In other languages randreset it not intended to be used in such a way. Why should Lumerical be so different?

      Can we get someone else to weigh in on this?

    • Guilin Sun
      Ansys Employee

      As I mentioned previously, if the seed is the same, rand will give the exactly the same result, which is designed in purpose. internally findmodes either succeeds or fails and it gives fixed result. 

      Unfortunately I do not believe such feature will be modified. Otherwise other examples will fail.

    • amitrotem2
      Subscriber

       

      If findmodes reset the seed this should be documented

       

    • Guilin Sun
      Ansys Employee

      Please note that findmodes does not change the seed of rand, and  this is why I suggest to use randreset to change the seed.

      I have tested and posted my testing result that the rand does not change its result after findmodes.  I thougt you want to have it reset. Otherwise rand works correctly after findmodes, as expected that its value keeps the same.

      if yours keeps change, please do a clean installation with the latest version: https://optics.ansys.com/hc/en-us/search?utf8=%E2%9C%93&query=clean+installation

      Again,my test shows it works fine .

       

       

    • amitrotem2
      Subscriber

      Please note, that this behaviour is not documented. So it's either a bug or an oversight. Either way, this needs fixing. Please fix it.

    • Guilin Sun
      Ansys Employee

      I do not understand what behavior you are referring to: after findmodes, rand does NOT change its value, as designed.

      findmodes;
      ?rand(); # same number
      findmodes;
      ?rand(); # same number
      findmodes;
      ?rand(); # same number
      findmodes;
      ?rand(); # same number
      result: 
      0.449832  
      result: 
      0.449832  
      result: 
      0.449832  
      result: 
      0.449832
      You said:
      "calling findmodes resets the random number generator, meaing that calling findmodes; rand(); multiple times gives the same output."
      it does not reset the random number genetor.

      Then you said:"

      The problem is that calling findmodes is like calling randreset

      This makes no sense "

       

      SO I do not understand which behavior you expect it to be?

      in the website, it says: "Generates a uniform random number between 0 and 1. In order to reset the generator seed use the command randreset. "
      https://optics.ansys.com/hc/en-us/articles/360034926413-rand-Script-command"
      Maybe be it should read like this:
      Generates a uniform random number between 0 and 1. In order to reset the generator, seed MUST use the command randreset.

      Therefore, after findmodes, rand keeps the same value , and it is designed to be.

       

    • amitrotem2
      Subscriber

      What do you mean "as designed"? what are you based on? I don't see it in the documentation.

      As far as I understand the documentation, rand should return different value each time I call it, unless I use randreset

      Based on the documentation;
      I expect for (j=1:5){?rand();} to print 5 different numbers, and so it does.
      I expect for (j=1:5){randreset(j); ?rand();} to print 5 different numbers, and so it does.
      I expect for (j=1:5){randreset(9); ?rand();} to print the same number 5 times, and so it does.
      And I expect for (j=1:5){findmodes; ?rand();} to print 5 different numbers (or any other function that is not randreset for that matter), but it does not.

    • Guilin Sun
      Ansys Employee

      "As designed", meaning it works like what you see: when the seed does not change rand will not change result.

      NO, it depends. try:

      ?rand;
      result: 
      0.536027  
      ?rand;
      result: 
      0.415442  
      ?rand;
      result: 
      0.955274  

      Please note again: findmodes returns a fixed value, it will NOT reset the seed for rand!!

       

       

    • amitrotem2
      Subscriber

      I think we have different terminology, so I want to clarify; when I say findmodes resets the seed, I mean calling findmodes will make rand predictable, in the sense that;

      findmodes;
      a = randmatrix(100);
      findmodes;
      b = randmatrix(100);
      ?all(a==b);

      will result in true.
      same as;

      randreset(7);
      a = randmatrix(100);
      randreset(7);
      b = randmatrix(100);
      ?all(a==b);

      This is what I mean by "findmodes resets the seed".

      You can call it whatever you like, but the point is that this behavior of "findmodes affecting rand in the same manner that randreset affect rand" is not documented.

    • Guilin Sun
      Ansys Employee

      Agree. I have difficulty to understand if you want the rand to give the same result or different result.

      When there is no other math operation before rand, it picks up a seed from the script background. so if you have two rand funcitons like this

      ?rand;

      ?rand;

      they will give different results as expected.

      However if there is a "findmodes", it outputs a specific data in the script work space so that 

      findmodes;

      ?rand;

      findmodes;

      ?rand;

      will give the same result. This is because findmodes itsself needs a random seeds and its output is fixed everytime in one workspace. Thus the rand function always picks up this fixed seed.

      Why this behavior is not document? because saldom people use "rand" function after "findmodes". "findmodes" function has its own way to get the random seed. There is no help to add "rand" after "findmodes". 

      As mentioned from my first reply, if you really want a random result after "findmodes", you will need to reset the seed for "rand".

       

       

    • amitrotem2
      Subscriber

      You know what they say, for every person asking about an issue there are many more that encountered it.
      So I think it would be helpful to note in the documentation of findmodes that it affects subsequent calls to rand.
      Would have definitely saved us this long thread.

      Are there any other functions that have the same effect on the seed?

    • Guilin Sun
      Ansys Employee

      It is now known as no other customer reports such use. 

      The documentation is feezed for a move so we will need to waite for a few weeks till it is online in the new host.

Viewing 17 reply threads
  • The topic ‘`findmodes` resets the random number generator’ is closed to new replies.