How do i create a random number generator between 5 and 9 for each for loop interation
ive tried
x=randi([5 9])
but it gives me a random number the first time, but then continues to run that number.
how do I make x more random?

댓글 수: 4

Bob Thompson
Bob Thompson 2019년 7월 15일
Did you put that line inside the loop, or generate beforehand?
Peyton Reddick
Peyton Reddick 2019년 7월 15일
I put rng('shuffle') before the for loop
otherwise the loop continues to run forever
Peyton Reddick
Peyton Reddick 2019년 7월 15일
I put x=randi([5 9]) inside the for loop
Steven Lord
Steven Lord 2019년 7월 15일
Show us a small sample of code containing your loop, how and where you initialize the generator, how and where you call randi, and where you plan to use that number.
If the body of the loop is very long, you don't need to show us all of it. Just show us the lines where you control the generator, generate numbers, and indicate where you use the number.

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

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 7월 15일

1 개 추천

iter_num=10; % change as per your requirement
x=[];
for i=1:iter_num
x(i)=randi([5 9])
end
or
iter_num=10; % change as per your requirement
for i=1:iter_num
x=randi([5 9])
end

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2019년 7월 15일

답변:

2019년 7월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by