![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1210358/image.png)
How can I make each iteration of a for each block create a unique random number?
4 ビュー (過去 30 日間)
古いコメントを表示
I want to create a random number for all iterations of my For Each Block at each time step. I've tried by simply adding a random number in my For Each Subsystem but I get the same random number outputted for each iteration. See a test model of this below.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1210348/image.png)
How can I achieve generating unique random numbers for each iteration?
One method I know exists would be to have it outside the for each loop but that is undesirable because:
- My model is several subsystems beneath the top level of the for each loop so it would be undesirable.
- I would like to create this into a library block so I don't want it to be dependent on signals at the top level.
0 件のコメント
採用された回答
cr
2022 年 11 月 28 日
You will have to generate a vector of random numbers outside and pass it as an input. Default partitioning of this vector might not happen the way you need it.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1210358/image.png)
Cheers.
2 件のコメント
cr
2022 年 11 月 28 日
Not sure if that is possible. The rng blocks spit a number (or a vector of numbers) in sequence for each time step. The iterations inside the foreach block are not individual time steps -just the same time step iterating over each element in the vector signal.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Sources についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!