Non continous range random number generation

Hi, I want to generate a random integer vector of range 1 to 10. The vector must contain all random integers in range except for 1 and 4. Regards, Muhammad Waqar Ahmed

1 件のコメント

Mahdi
Mahdi 2013 年 4 月 24 日
Do you want to generate just one random number from 1 to 10? Or what is the length of the vector that you want to produce?

回答 (2 件)

Walter Roberson
Walter Roberson 2013 年 4 月 24 日

0 投票

V = [2, 3, 5, 6, 7, 8, 9, 10];
randV = V(randperm(length(V)));

3 件のコメント

Muhammad
Muhammad 2013 年 4 月 25 日
Excellent!!! you are the man this worked :D
Muhammad
Muhammad 2013 年 4 月 25 日
How about if i want to generate one number at a time?
Walter Roberson
Walter Roberson 2013 年 4 月 25 日
V = [2, 3, 5, 6, 7, 8, 9, 10];
randV = V(randi(length(V)));
Mahdi
Mahdi 2013 年 4 月 24 日

0 投票

If you have the statistics toolbox, you can use the following idea:
randsample([2 3 5 6 7 8 9 10], 5, 1)
This produces 5 random numbers from the set, with replacement.

この質問は閉じられています。

質問済み:

2013 年 4 月 24 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by