Generating random numbers in Simulink with MATLAB function-block

Zantonius

I've tried to both google this question and search among the questions and answers here, but I've found no definitve answer to my question so I'm making a new one. Hopefully it won't be too much trouble!

I'm creating a simulation in Simulink where I have a "MATLAB function"-block that is supposed to take input from another source (we can consider this source a "Constant"-block) and then apply a random number that is generated from the MATLAB function-block on the input.

My problem is that I get the exact same randomized numbers every single time I run the Simulink simulation. And I was wondering if someone could help me solve my problem?

Here is the code (not all of it, but all of it that matters):

% function MC_output = randomizer(Stat_input)
%#codegen    minrand = 0.1;
    maxrand = 1.9;
    points = 10;    
    rand_numbers = Stat_input*minrand + rand(1, points).*(maxrand-minrand);
    MC_output = mean(rand_numbers);
end

I've read about this solution:

coder.extrinsic('rng');
rng('shuffle');

I've used it in different ways but with no success. Some help would be greatly appriciated! Oh, and btw, I'm using MATLAB R2012a.

Thanks in advance, Niklas

Phil Goddard

The rand being called from your MATLAB Fcn Block is not the same rand as would be called from MATLAB, hence the reason why rng('shuffle'); has no effect on Simulink's random number generation.

You could force the MATLAB Fcn Block to use MATLAB's rand function by doing the equivalent to,

function y = fcn
%#codegen
coder.extrinsic('rand','rng');
y = 0;

persistent atTime0
if isempty(atTime0)
    rng('shuffle');
    atTime0 = false;
end

y = rand;

Or you can use the old style method for resetting the random number's seed

function y = fcn(seed)
%#codegen

persistent atTime0
if isempty(atTime0)
    rand('seed',seed);
    atTime0 = false;
end

y = rand;

But the easier approach is to feed the random number/vector in as an input generated by the Uniform Random Number Generator block, with its seed parameter being set randomly (using MATLAB's rand function).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Generating random numbers in Simulink with MATLAB function-block

From Dev

Editing the Code of a "MATLAB Function" Block in Simulink Programmatically

From Dev

How to use syms in Simulink Matlab function block

From Dev

OOP Matlab inheritance issue with Matlab coder (Simulink MATLAB Function block)

From Dev

Generating random numbers and a sequence key in MATLAB

From Dev

Generating multivariate normally distributed random numbers in Matlab

From Dev

Generating multivariate normally distributed random numbers in Matlab

From Dev

Generating random numbers rounded to hundredths place in MATLAB

From Dev

C rand() function is not generating random numbers

From Dev

View class properties while debugging matlab function block in simulink

From Dev

Plotting inside Matlab Function Block for real time signals in Simulink

From Dev

Compiling error due to variable size Matrix in simulink (matlab function block)

From Dev

Function of memory block in Simulink

From Dev

Function of memory block in Simulink

From Java

Generating random numbers with Swift

From Dev

Generating random numbers in Haskell

From Dev

Generating unique random numbers

From Dev

Generating weighted random numbers

From Dev

Generating random poll numbers

From Dev

Generating groups of random numbers

From Dev

Generating unique random numbers

From Dev

Generating a set of random numbers

From Dev

Generating sequence of random numbers

From Dev

Generating random numbers randomly

From Dev

A Bug in MATLAB in the simulink serial acquisition block in MATLAB

From Dev

generating random numbers in python with percentage function on what selected value in python

From Dev

F# function conciseness when generating random numbers

From Dev

My function for generating random numbers isn't going past 500000

From Dev

Continuos Signal to MATLAB Function in Simulink

Related Related

  1. 1

    Generating random numbers in Simulink with MATLAB function-block

  2. 2

    Editing the Code of a "MATLAB Function" Block in Simulink Programmatically

  3. 3

    How to use syms in Simulink Matlab function block

  4. 4

    OOP Matlab inheritance issue with Matlab coder (Simulink MATLAB Function block)

  5. 5

    Generating random numbers and a sequence key in MATLAB

  6. 6

    Generating multivariate normally distributed random numbers in Matlab

  7. 7

    Generating multivariate normally distributed random numbers in Matlab

  8. 8

    Generating random numbers rounded to hundredths place in MATLAB

  9. 9

    C rand() function is not generating random numbers

  10. 10

    View class properties while debugging matlab function block in simulink

  11. 11

    Plotting inside Matlab Function Block for real time signals in Simulink

  12. 12

    Compiling error due to variable size Matrix in simulink (matlab function block)

  13. 13

    Function of memory block in Simulink

  14. 14

    Function of memory block in Simulink

  15. 15

    Generating random numbers with Swift

  16. 16

    Generating random numbers in Haskell

  17. 17

    Generating unique random numbers

  18. 18

    Generating weighted random numbers

  19. 19

    Generating random poll numbers

  20. 20

    Generating groups of random numbers

  21. 21

    Generating unique random numbers

  22. 22

    Generating a set of random numbers

  23. 23

    Generating sequence of random numbers

  24. 24

    Generating random numbers randomly

  25. 25

    A Bug in MATLAB in the simulink serial acquisition block in MATLAB

  26. 26

    generating random numbers in python with percentage function on what selected value in python

  27. 27

    F# function conciseness when generating random numbers

  28. 28

    My function for generating random numbers isn't going past 500000

  29. 29

    Continuos Signal to MATLAB Function in Simulink

HotTag

Archive