필터 지우기
필터 지우기

random no.

조회 수: 2 (최근 30일)
huda nawaf
huda nawaf 2012년 3월 18일
hi, can i get random no. in specific range ?
between 19 , -19
thanks
  댓글 수: 1
Oleg Komarov
Oleg Komarov 2012년 3월 18일
Yes, you can.

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

답변 (4개)

Image Analyst
Image Analyst 2012년 3월 18일
You didn't read the help on rand(), did you? If you had, you would have seen the answer to your question as the very first example:
Example 1
Generate values from the uniform distribution on the interval [a, b]:
r = a + (b-a).*rand(100,1);
Needless to say, a=-19 and b = 19 in your case, and 100 is the number of random numbers to be generated, or:
r = -19 + 38 .* rand(100, 1);
  댓글 수: 1
Jan
Jan 2012년 3월 18일
While the solution in easy, not to say trivial, the suggestion to read the documentation is an will be valuable. +1

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


the cyclist
the cyclist 2012년 3월 18일
Depending on whether you want to draw from a discrete or a continuous distribution, you will likely want to use either the randi() or unifrnd() function.

Sean Lawson
Sean Lawson 2012년 3월 18일
naively, this works: 38*(rand-0.5)

Sahyadri Vibhu
Sahyadri Vibhu 2012년 3월 20일
As Sean Lawson said in the above comment, using -19+38*rand will generate a random real number in the range (-19 19)
to generate random integers use
randi ([lower_limit upper_limit],a,b,c...)
which generates an axbxcx..... random integer array in the range (lower_limit upper_limit)

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by