Clear Filters
Clear Filters

How to insert a pre-generated set of random numbers into a simulink simulation

3 views (last 30 days)
Dear Matlab Experts,
I am curious if this case is possible, and what would be the solution.
  • I generate a set of 10 random numbers A, consisting of numbers between 0-100 into the workspace
  • I generate another set of 10 random numbers B, consisting of integers between 1 and 2 into the workspace
  • I insert the generated random numbers in my simevents model, specificly into an entity server, in the events actions space, where it will use these generated numbers from set A and set B for simulation purposes
  • How the entity servers takes the numbers from set A and set B will be one by one (ex. 1st number from set A is 89 and 1st number from set B is 1. and the next loop it takes 2nd number generated from each set) for each loop (a single loop takes 7 time steps, and I have 70 time steps in total, so 10 loops)
Hope my question is clear, but kindly let me know if you need further information regarding my simulation.
Thank you in advance!

Answers (1)

Jaimin
Jaimin on 2 Sep 2024
As per my understanding you want to generate two sets of random numbers: "Set A" with 10 numbers between 0-100, and "Set B" with 10 integers between 1-2. These numbers should be used in an entity server within the model, where each loop of 7 timesteps uses corresponding numbers from "Sets A and B" sequentially over 10 loops (totalling 70 time steps).
To accomplish this, we can utilise the "MATLAB Function" Block in Simulink. I've included a sample function to help illustrate the process.
function [outputA, outputB] = getNumbers(loopIndex)
%#codegen
persistent A B
if isempty(A) || isempty(B)
A = evalin('base', 'A'); % Access A from the base workspace
B = evalin('base', 'B'); % Access B from the base workspace
end
% Ensure loopIndex is within bounds
index = mod(loopIndex - 1, 10) + 1;
outputA = A(index);
outputB = B(index);
end
Since we have 70 timesteps and each loop takes 7 timesteps, you can iterate through the indices by incrementing the loop index every 7 steps.
For more information, please refer to this MathWorks documentation.
Implement MATLAB Functions in Simulink with MATLAB Function Blocks:
I hope this will be helpful.
  1 Comment
Aaron Bramhasta
Aaron Bramhasta on 2 Sep 2024
Hi @Jaimin thank you for your reply.
Yes, you understood correctly my question. I do have one question regarding your solution.
The matlab function requires an input of loopIndex (as pictured above), which I assumes requires something to trigger it, in this example everytime a loop is completed and continues to the next one. From the documentation you provided, it gave an example of a constant block as the input, but with my simevents entity blocks, that operates on entities instead of signal, how would I trigger the input of loopIndex?
I also just noticed from my model, that a single loop can be 5-7 timesteps, depending on how many entity servers it goes through. So instead of incrementing the loopIndex every 7 timesteps, would there be a way to increment the loopIndex everytime an entity starts another loop?
Once again, thank you so much, let me know if you need further information regarding my model.

Sign in to comment.

Categories

Find more on Discrete-Event Simulation in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!