Hello every one I would like to generate a series of random odd numbers with in a specific range for example the range from 1 to 20 the odd number for example 1,3,5,7,9,11,13,15,17,19

 채택된 답변

Star Strider
Star Strider 2014년 10월 12일
편집: Star Strider 2014년 10월 12일

0 개 추천

My approach:
x = 1:20;
odv = x(rem(x,2)==1);
n = 10; % Length Of Series
oddrnd = odv(randi(length(odv),1,n));

댓글 수: 9

Image Analyst
Image Analyst 2014년 10월 12일
편집: Image Analyst 2014년 10월 12일
Almost the same as my code - you beat me to it:
oddNumbers = 1 : 2 : 19; % End at whatever number you want.
numbersToGenerate = 100; % Whatever you want.
values = oddNumbers(randi(length(oddNumbers), 1, numbersToGenerate))
Star Strider
Star Strider 2014년 10월 12일
I know that feeling only too well!
aya
aya 2014년 10월 12일
It works fine but it gave me a matrix how can I get one value each time???????
To get one value each time, set:
n = 1;
aya
aya 2014년 10월 12일
I was just trying that and it works thanx
Star Strider
Star Strider 2014년 10월 12일
My pleasure!
Image Analyst's code is nicer. Star Strider seemed to forget you can create an odd array by using
1:2:20
Star Strider
Star Strider 2014년 10월 12일
I didn’t forget, and I considered the (2*n+1) possibility as well. I simply chose the most obvious solution.
David Barry
David Barry 2014년 10월 12일
I would argue that 1:2:20 is more obvious and beginner friendly than your solution.

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

추가 답변 (6개)

Guillaume
Guillaume 2014년 10월 12일
편집: Guillaume 2014년 10월 12일

1 개 추천

How about?
(randi(10, 1, serieslength) - 1) * 2 + 1; %replace serieslength by actual length of series.
That is generate random integers between 0 and 9, multiply by 2 and add 1.

댓글 수: 2

aya
aya 2014년 10월 12일
its generate integers between 0:20? How I make generate randoms with in the values I that I want
Guillaume
Guillaume 2014년 10월 12일
편집: Guillaume 2014년 10월 12일
It generates odd numbers between 1 and 19. It's not particularly hard to figure out how to do it for other ranges
halfrange = (maxrange - minrange) / 2; %replace max and min by actual values, values must be odd.
(randi(halfrange, 1, serieslength) - 1) * 2 + minrange;

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

Noor elhamed
Noor elhamed 2017년 4월 28일

0 개 추천

How to write the code using user defined function?
Gorret Komuhendo
Gorret Komuhendo 2021년 2월 8일

0 개 추천

How do I find the MATLAB command which generate the following lists 1. The odd number 1,3,...99 2. The numbers 10,20,30,...120? Can you help me please
Chathurni Ratwatte
Chathurni Ratwatte 2021년 5월 17일

0 개 추천

Create the vector consisting of the whole odd numbers between 15 and 27
FAISAL
FAISAL 2023년 4월 9일

0 개 추천

Create a scalar of an odd number

댓글 수: 1

for K = 1 : 5
scalar = randi([0 65535]) * 2 + 1
end
scalar = 120775
scalar = 130513
scalar = 43441
scalar = 97309
scalar = 26623

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

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

질문:

aya
2014년 10월 12일

댓글:

2023년 4월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by