Generation of an Odd numbers only

66 ビュー (過去 30 日間)
aya
aya 2014 年 10 月 12 日
コメント済み: Walter Roberson 2023 年 4 月 9 日
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 日
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
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 件)

Sameh elmedawi
Sameh elmedawi 2022 年 3 月 15 日
a = 1:2:2*n

Guillaume
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 件のコメント
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 日
How to write the code using user defined function?

Gorret Komuhendo
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
  1 件のコメント
Image Analyst
Image Analyst 2022 年 2 月 5 日
v1 = 1 : 2 : 99
v2 = 10 : 10 : 120

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


Chathurni Ratwatte
Chathurni Ratwatte 2021 年 5 月 17 日
Create the vector consisting of the whole odd numbers between 15 and 27
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 2 月 5 日
[17 19 21 23 25]

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


FAISAL
FAISAL 2023 年 4 月 9 日
Create a scalar of an odd number
  1 件のコメント
Walter Roberson
Walter Roberson 2023 年 4 月 9 日
for K = 1 : 5
scalar = randi([0 65535]) * 2 + 1
end
scalar = 120775
scalar = 130513
scalar = 43441
scalar = 97309
scalar = 26623

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by