Random Number range incorrect

Hello,

I am running into a strange issue, using 4.9.7 on a LAUNCHXL-F28379D. Using the Random Numbers block, this however does not seem to give a number within the range (min,max) rather a much smaller range. When given a range 0,1. The output seems to be (0,~0.4). The seed is set to 0.5 and sample time to 0.

Kind regards,
Will

Hello, any chance you can share a model that would make it easier to understand/recreate the issue?

Hi Mundair,

Please find the plecs script attached and the output that I am getting on my machine.

Thanks!

Variable_f_switch.plecs (9.3 KB)

Thanks for the info. I have the same version of PLECS on my machine and ran your model with the parameters you specified but I am unable to replicate your results. Here is what I am getting:

Let me investigate a little more to see if I’m able to recreate the issue on another machine. Could you specify what machine you are using:

  • Mac vs Windows
  • OS version number
  • CPU chip (e.g. Apple M4)

Hello,

I am running this on embedded hardware, the LAUNCHXL-F28379D. But am compiling on my windows 10 machine with an intel i7.

Thanks.

Hi, thanks for clarifying that it’s when you are running on the MCU - I had missed that part!

The issue you are running into is based on how the random number generator is implemented in PLECS. All blocks in PLECS are open to inspection unless they are are basic building blocks (e.g. gains, addition, integrator, etc). If you look under the mask of how the random number generator is implemented in PLECS you’ll notice that it returns the results as a double while the MCU only supports float. So while code generation can makes it easy to take your controls and flash it onto the MCU these nuances can get you into trouble. This is specially true when you are generating code for a model that contains C-Scripts. You have to be very careful that the code implementing your feature can be supported by the HW you are going to be generating code for.

While in offline simulation you don’t have to worry about how long the algorithm might take to compute as the simulation is not running in realtime in the microcontroller you might run into issues or see unexpected results.

May I ask what is the purpose of using a random number generator in your model? Maybe there is an alternate solution that might be easier for the microcontroller to handle.

Hi Will,

The issue you’ve run into is being looked at by the developers and will be fixed with the release of PLECS 4.9.8.

1 Like