Random specified numbers.

조회 수: 1 (최근 30일)
Eivind Jamessen
Eivind Jamessen 2020년 2월 21일
댓글: Giuseppe Inghilterra 2020년 2월 21일
Hi, I want to print random numbers from 1 to 100, but i want to specifiy that most of the numbers should be around 20. In other words, it is most likely to get 20, but other numbers can also occur. Is this possible?

답변 (1개)

Giuseppe Inghilterra
Giuseppe Inghilterra 2020년 2월 21일
Hi,
you can use randsample matlab function. (Reference: https://www.mathworks.com/help/stats/randsample.html).
y = randsample(n,k,true,w);
where:
  • n is the maximum integer random value generated;
  • k is number of random values that you want generate;
  • w is weigth vector that contains n weights.
In your case n = 100 and you must define w vector with 100 weights (value 20 will be more weighted) such that weight sum is 1.
Hope this helps.
  댓글 수: 3
Adam
Adam 2020년 2월 21일
e.g.
x = 1:100;
w = exp( -( x - 20 ).^2 / 1000 );
w = w / sum( w );
Giuseppe Inghilterra
Giuseppe Inghilterra 2020년 2월 21일
Adam's example is a good one to follow in order to create in a easy and fast way weight vector.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by