How can I generate a single random number?

조회 수: 93 (최근 30일)
Anish
Anish 2013년 9월 3일
댓글: Steven Lord 2020년 3월 27일
I need to generate a single random number within my code. Is there a function to do this?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 3일
편집: Azzi Abdelmalek 2013년 9월 3일
rand % random number between 0 and 1
%or
randi(100) % integer random number between 1 and 100
  댓글 수: 2
Samuel Katongole
Samuel Katongole 2020년 3월 27일
That's is fine for the range 0 to 100; but how about with no range, say for, instance, from 0 to infinity?
Steven Lord
Steven Lord 2020년 3월 27일
The maximum value you can store in double precision is realmax, which is about 1e307. So no, you can't generate double precision values greater than that (or you can, they just turn out to be Inf.)
>> x = 1e500
x =
Inf
Can you generate an arbitrary double precision number between 0 and realmax? There's no function in MATLAB to do so.
Can you share some details about what you're trying to do with these numbers "with no range"? There may be a way to do what you want that doesn't run into this problem of overflowing to Inf.

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

추가 답변 (0개)

카테고리

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