How to generate random vector with boundary limit and with fixed sum? Also ensure to same value when called the function with same parameters.

3 ビュー (過去 30 日間)
I want to generate random vector between 0 and 1 with fixed sum. I also want to ensure that if i run the function with same value, then it will return me the same values.
I have used the below function but it is returning different vector each time.
Roger Stafford (2021). Random Vectors with Fixed Sum ( https://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum ), MATLAB Central File Exchange. Retrieved May 10, 2021.

回答 (1 件)

KSSV
KSSV 2021 年 5 月 10 日
rand creates random number every time. If you fix the seed, then you will get the same random number. Read about rng.
  2 件のコメント
SANDEEP SINGH RANA
SANDEEP SINGH RANA 2021 年 5 月 10 日
編集済み: SANDEEP SINGH RANA 2021 年 5 月 10 日
When i will first time, I want 5 random values whose sum is 1. In Second time, I want 3 random values whose sum is 1. In third time, I want 5 random values whose sum is 1. ( same random number as generate in first time because this time also we want 5 random number). In fourth time, I want 10 random values whose sum is 1. [likewise it should generate]
Bruno Luong
Bruno Luong 2021 年 5 月 10 日
編集済み: Bruno Luong 2021 年 5 月 10 日
The question is more about programing workflow than thet task you want to achieve (random with fixed sum). What you want is soring the resullt for the certain input parameters and use it later when the same inputs are recall.
it seems MATLAB recently add a capability to check function input is the same of what has been passed before than return the same result or do the calculation then store the result. Can't recall the doc page or syntax of such feature.
Otherwise you can use containers.Map object and put such object as persistent variable.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by