How to write program to choose random numbers from given input set?

1 回表示 (過去 30 日間)
reshdev
reshdev 2014 年 8 月 29 日
回答済み: Image Analyst 2014 年 8 月 29 日
i want to write two programs ----
a program that can randomly choose number T1 from [5, 10, 15] and if
if it choose T1=5, then it should give T2= 6
if it choose T1=10, then it should give T2= 11
if it choose T1=15, then it should give T2= 16
So, T2 is addition of 1 to T1
second program-----
if i have set of values P=(1,2,3,......10)
how to pick randomly P1 and P2 from above set such that P1 and P2 are not same.

採用された回答

Star Strider
Star Strider 2014 年 8 月 29 日
編集済み: Star Strider 2014 年 8 月 29 日
Seems like homework.
My code:
X = 5:5:15;
T1 = X(randi(3));
T2 = T1+1;
P1 = X(randi(3));
P2 = setdiff(X,P1);
P2 = P2(randi(2));
  2 件のコメント
reshdev
reshdev 2014 年 8 月 29 日
Thank You. just to let you know, second program has nothing to do with first one. Sorry, i know it could be simple program but i am new to matlab.
Star Strider
Star Strider 2014 年 8 月 29 日
My pleasure!

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2014 年 8 月 29 日
Try this:
% Program 1
clc;
% Define the choices:
choices = [5, 10, 15];
% Get T1 at random.
T1 = choices(randi(3,1,1))
% Get T2, which is one plus T1
T2 = T1 + 1;
% Program 2
P = 1:10
% Get two random locations that are different.
randomIndexes = randperm(length(P), 2)
% Extract the two values.
P1 = P(randomIndexes(1))
P2 = P(randomIndexes(2))

Evan
Evan 2014 年 8 月 29 日
see randi

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by