TAGGED: bug, Bug report, FDE
-
-
February 22, 2024 at 7:10 pm
amitrotem2
Subscribercalling `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
``` -
February 23, 2024 at 6:22 pm
Guilin Sun
Ansys Employeefindmodes 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.
Â
-
February 23, 2024 at 7:08 pm
amitrotem2
SubscriberWhat you said makes no sense to me.
The problem is that calling findmodes is like calling randreset
This makes no senseÂ
-
February 23, 2024 at 7:22 pm
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?
Â
-
February 23, 2024 at 7:45 pm
amitrotem2
SubscriberI 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.
-
February 23, 2024 at 9:12 pm
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.
Â
Â
-
February 24, 2024 at 6:04 am
amitrotem2
SubscriberNowhere 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?
-
February 26, 2024 at 4:55 pm
Guilin Sun
Ansys EmployeeAs 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.
-
February 26, 2024 at 5:40 pm
amitrotem2
SubscriberÂ
If findmodes reset the seed this should be documented
Â
-
February 26, 2024 at 7:31 pm
Guilin Sun
Ansys EmployeePlease 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 .
Â
Â
-
February 27, 2024 at 4:46 am
amitrotem2
SubscriberPlease note, that this behaviour is not documented. So it's either a bug or an oversight. Either way, this needs fixing. Please fix it.
-
February 27, 2024 at 7:19 pm
Guilin Sun
Ansys EmployeeI 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:
"callingfindmodes
resets the random number generator, meaing that callingfindmodes; 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.
Â
-
February 28, 2024 at 3:11 pm
amitrotem2
SubscriberWhat 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 userandreset
Based on the documentation;
I expectfor (j=1:5){?rand();}
to print 5 different numbers, and so it does.
I expectfor (j=1:5){randreset(j); ?rand();}
to print 5 different numbers, and so it does.
I expectfor (j=1:5){randreset(9); ?rand();}
to print the same number 5 times, and so it does.
And I expectfor (j=1:5){findmodes; ?rand();}
to print 5 different numbers (or any other function that is notrandreset
for that matter), but it does not. -
February 28, 2024 at 3:27 pm
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!!
Â
Â
-
February 28, 2024 at 6:16 pm
amitrotem2
SubscriberI 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.
-
February 29, 2024 at 5:51 pm
Guilin Sun
Ansys EmployeeAgree. 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".
Â
Â
-
March 4, 2024 at 8:02 am
amitrotem2
SubscriberYou 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 offindmodes
that it affects subsequent calls torand
.
Would have definitely saved us this long thread.
Are there any other functions that have the same effect on the seed? -
March 4, 2024 at 3:42 pm
Guilin Sun
Ansys EmployeeIt 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.
-
- The topic ‘`findmodes` resets the random number generator’ is closed to new replies.
- Difference between answers in version 2024 and 2017 lumerical mode solution
- Import material .txt file with script
- Absorption cross-section of AuNR excited by prism-based TIR
- How to measure transmission coefficients on a given plane .
- TE Polarization Fraction + Waveguide TE/TM Fraction
- Edge Coupler EME Example Issue
- Lumerical Python API Topology Optimization Error 2025 R1
- 2D inverse design grating coupler
- FDE : Simulation waveguides with electrodes
- Loss of lithium niobate phase modulator
-
2432
-
930
-
599
-
591
-
569
© 2025 Copyright ANSYS, Inc. All rights reserved.