rand
ではなく (0 から 1 の間の数値を選択)を使用する単純な実装を次に示しrandi
ます。
buff=1000; % number of random numbers to test with each iteration...
yn=1;
while yn
num=input('Enter a number between 0 and 1 >> ');
nn = -buff;
found=[];
while isempty(found)
nn= nn+buff;
found=find(rand(buff,1)>num,1,'first');
end
nn=nn+found;
disp(nn)
yn=input('Would you like to try again? (0=no,1=yes) >> ');
end
変数nn
には、最初の成功までの試行回数が含まれます。
テストの実行は次のようになります。
Enter a number between 0 and 1 >> 0.999
1325
Would you like to try again? (0=no,1=yes) >> 0
使用する変更randi
は簡単です。