Generation of an Odd numbers only
63 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Star Strider
2014-10-12
编辑:Star Strider
2014-10-12
My approach:
x = 1:20;
odv = x(rem(x,2)==1);
n = 10; % Length Of Series
oddrnd = odv(randi(length(odv),1,n));
9 个评论
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
2014-10-12
I would argue that 1:2:20 is more obvious and beginner friendly than your solution.
更多回答(6 个)
Guillaume
2014-10-12
编辑:Guillaume
2014-10-12
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 个评论
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;
Gorret Komuhendo
2021-2-8
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
2021-5-17
Create the vector consisting of the whole odd numbers between 15 and 27
FAISAL
2023-4-9
Create a scalar of an odd number
1 个评论
Walter Roberson
2023-4-9
for K = 1 : 5
scalar = randi([0 65535]) * 2 + 1
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!