How do I get random numbers from a Simulink Coder executable?
1 view (last 30 days)
Show older comments
I have a simple Simulink model that consists of a Random Number block and a To File block that writes the random number to a file. I compile this using Simulink Coder and get an executable. Every time I run the executable I get the same number. I would like to find a way to make it so that every time I run the executable I get a different number.
There was a similar problem asked here which was apparently solved, though the solution was not given. I assume that the seed is being fixed during the compilation, but I can't figure out how to change that.
If anyone has a solution I would appreciate your help. David
0 Comments
Accepted Answer
Kaustubha Govind
on 14 Feb 2013
It looks like the 'Seed' parameter is a non-tunable parameter, so I'm not sure how you would do this, other than manually modify the code and add a wrapper around it to get the Seed value as a command-line parameter, or perhaps compute it based on the system clock or something. (If it were a tunable parameter, you could just declare it as an ImportedExtern parameter that your wrapper code could then define)
However, in general, if you plan to use the generated code in your own application, you would probably generate a DLL from the model, instead of an executable. In case of a DLL, it is initialized once, and then executed multiple times before termination. In this case, since the seed is initialized only once, a different random value should be obtained for each execution.
3 Comments
More Answers (1)
Azzi Abdelmalek
on 14 Feb 2013
Edited: Azzi Abdelmalek
on 14 Feb 2013
You create a mat file fic_seed.mat
seedv=1:300
save fic_seed seedv
In model properties-callbacks-Init Fcn write this code
v=load('fic')
v=v.v;
val=v(1);
v(1)=[];
save fic v
set_param('yoursimulinkmodelname/Random Number','seed',num2str(val))
Random Number is the name of your random block, Check if it's the same name in your simulink model.
1 Comment
Kaustubha Govind
on 14 Feb 2013
Azzi: Since David wants to do this in the generated code, the InitFcn callback won't help unfortunately, since it is only called within the Simulink environment.
See Also
Categories
Find more on Simulink Coder in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!