Hi all,
I am trying to implement random number generation in my model. Following the custom task development guide gives some errors unfortunately. Specificly, RAND_MAX is not resolved, despite including the stdlib header. Also, scoping the return value of rand() points to usage of the MATLAB version of the function as it shows 0.841 (rather than an integer between 0 and RAND_MAX as in the C version).
There was an earlier thread to this issue but it didnt suggest any solutions.
Kind regards,
Matthias
1 Answers
Hi Koloman,
you were absolutely right about the action language. Indeed, Matlab was selected instead of C.
Switching to C however introduced some new problems about my variables naturally (and probably various other things regarding syntax that didnt pop up immediately). Instead of solving those I opted to try the Matlab version of rand() with the suggested random seed approach and it worked out perfectly.
Thanks a lot!
Matthias
Please login or Register to submit your answer
PS. I tried using Matlab´s random functions as a workaround but they are using the same seed every time I run my task.
Hi Matthias,
Since Stateflow isn’t finding definitions in the stdlib header and rand() is acting like the MATLAB function, it could be that your Stateflow is set to use MATLAB as the “action language” instead of C. The action language setting determines whether you use MATLAB or C as the programming language in Stateflow.
You can check what the action language is by opening Model Explorer and clicking on the name of your Stateflow chart and on the right you’ll find the Action Language drop down.
If it’s set to MATLAB, you can choose to continue to use MATLAB in Stateflow without problems. If it’s set to C, then your problem is different and we can investigate further.
Cheers,
Koloman
Hi Matthias,
I’ll also add that if you want to use the MATLAB random function, you need to seed it with a random seed. There is a From tag called ‘seed’ that will give you a random seed (unless your task protocol has that turned off) that you can pass into a call of rng()
For an example, there is a use of the MATLAB random number generation in the example task here: https://kinarm.com/task/coherent-dots-discrimination-task/
Cheers,
Koloman