Im trying to get a column vector with a list of composite numbers within a random range using the randi function but I keep getting an error.

2 ビュー (過去 30 日間)
My project is creating two BINGO cards that compete with eachother to see who wins first. The columns need to consist of composite numbers within a 20 number range (for B column its composite numbers that are in 1-99 but I must create a column vector to find all composite numbers within a 20 number range within the 1-99 range).
Here is code for B column vector for card 1:
%getting numbers in random range for both card1 B column
randB1 = randi([1 99]);
i = 0;
while i < 1
if (randB1 - 20) < 0
randB1 = randi(length(B));
else
rangeB1 = [randB1-20 randB1]; %checking to make sure first range number isnt <0
i= i +1;
end
end
%organizing range to put in Bcol1
if isprime(rangeB1(1))
rangB1=find(c==rangeB1(1)+1);
else
rangB1=find(c==rangeB1(1));
end
if isprime(rangeB1(2))
rangB2=find(c==rangeB1(2)-1);
else
rangB2=find(c==rangeB1(2));
end
Bcol1=c(rangB1:rangB2)';
randB2 = randi([1 99]);
This is the error I'm getting:
Error using randi
First input must be a positive scalar integer value IMAX, or two integer values [IMIN IMAX] with IMIN less than or equal to IMAX.
Error in Project (line 289)
r2 = randi([1,length(Bcol2)]);
  10 件のコメント
Michael Price
Michael Price 2021 年 7 月 30 日
Would I then put temp inside of Bcol1 = c( )';?
Walter Roberson
Walter Roberson 2021 年 8 月 2 日
temp = sort([rangB1, rangB2])
Bcol1 = c(temp(1):temp(2)).';

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

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 8 月 17 日
for K = 1:20:99
range = K:K+19;
composites = setdiff(range(~isprime(range)),1); %1 is never prime
composites(randperm(length(composites),5))
end
ans = 1×5
15 10 9 18 8
ans = 1×5
25 30 39 32 34
ans = 1×5
42 50 52 49 58
ans = 1×5
69 65 63 62 66
ans = 1×5
95 88 91 90 82

カテゴリ

Help Center および File ExchangeString についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by