I need to generate one random integer.
I tried these codes:
X = randi(1)
X = rand(1)
However, it only gives random numbers between 0 and 1 (like 0.2567, 0.9432, etc.).
I am hoping to get a random number from negative infinity to positive infinity.
What function should I use to be able to generate one random integer?
Thanks in advance!

5 件のコメント

David
David 2024 年 1 月 28 日
Interestingly and confusingly; X = randi(2), for example, returns either a 1 or a 2, but note that the integer test on the outcome is false; i.e., the outcome is not recognized as integer.
>> X = randi(2)
X = 1
>> Xint = isinteger(X)
Xint = logical 0
Where does the problem lie; i.e. in which function, the generator or the test?
Walter Roberson
Walter Roberson 2024 年 1 月 28 日
isinteger() tests for integer data type, not for integer value.
DGM
DGM 2024 年 1 月 28 日
編集済み: DGM 2024 年 1 月 28 日
The isinteger() function tests the numeric class of the input; it doesn't test for mod(x,1) == 0.
x = randi(1000,1,1) % this is double
x = 51
mod(x,1) % it's an integer
ans = 0
isinteger(x) % but it's not integer-class
ans = logical
0
isfloat(x) % it's floating-point instead
ans = logical
1
x = int16(x); % now it's integer-class
isinteger(x)
ans = logical
1
David
David 2024 年 1 月 28 日
Right, I agree that isinteger is testing that a value is a member of the "integer class", but if randi purports to produce pseudo-random integers, then shouldn't results me members of the interger class without explaicitly declaring that they are; i.e., without the need for the "int16" declaration. (I guess not, and I appreciate your response, but if a function says it is returning an integer, then in my book, it should actually return an integer that tests "true" for being a member of that class. That's why I think this is confusing.
Dyuman Joshi
Dyuman Joshi 2024 年 1 月 28 日
There's a difference between the integer as a data type and integer as a numerical entity.
The function randi() does return an integer, but, by default, it is of class double.

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

 採用された回答

KSSV
KSSV 2020 年 7 月 3 日
編集済み: KSSV 2020 年 7 月 3 日

2 投票

As you cannot fix infinity, decide a huge/large number ..say 10^5 and use:
x = randi([-10^5 10^5],1)

1 件のコメント

Roxanne Esguerra
Roxanne Esguerra 2020 年 7 月 3 日
Thanks this is a great help!

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 7 月 3 日
編集済み: Walter Roberson 2024 年 1 月 28 日

1 投票

The following generates integers natively.
typecast(randi([0 255],1,8,'uint8'),'int64')
The range is all possible integers between -2^63 and +2^63-1
infinity cannot be generated with this setup, as infinity is not part of the integer system.
To generate integers outside of the range indicated above, you need to switch to double(), but if you do that, you only have 53 bits of precision available, and all values outside the range +/- 2^53 are integers that are multiplied by a power of 2 -- you could not, for example, generate 2^53 + 17 . By 1E50, adjacent representable values are 2^114 apart. Is that what you want to deal with?

1 件のコメント

Roxanne Esguerra
Roxanne Esguerra 2020 年 7 月 3 日
Thanks, I appreciate the help!

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

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by