Trying to create a program that asks user to input a number (integer) and then generates a list of randomly generated numbers (integers) between ‘1’ and number that user entered. Their should not be any repetition of numbers in the list.
I have this so far and I can't seem to figure out how to not repeat the #'s and to display the inputted integer. I know I have to use a if command but don't know how to execute! I have attempted to use if and else statements but I am stuck.
clc;
x = input('Enter a integer: ');
for x=1:x,
k=round(rand*x);
fprintf('%d\n',k)
end
Any Help is most appreciated.

1 件のコメント

Walter Diolosa
Walter Diolosa 2011 年 12 月 13 日
Output of script should look as follows when input is entered.
Enter and Integer: 10
ans=
5
1
10
2
4
8
3
6
7
9
Thanks

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

 採用された回答

Wayne King
Wayne King 2011 年 12 月 13 日

0 投票

x = input('Enter your number\n');
randperm(x)

6 件のコメント

Sean de Wolski
Sean de Wolski 2011 年 12 月 13 日
Faster fingers!
Walter Diolosa
Walter Diolosa 2011 年 12 月 13 日
Output of script should look as follows when input is entered.
Enter and Integer: 10
ans=
5
1
10
2
4
8
3
6
7
9
Wayne King
Wayne King 2011 年 12 月 13 日
you mean just
randperm(x)'
Wayne King
Wayne King 2011 年 12 月 13 日
you can just do
x = input('Enter your number\n');
randperm(x)'
Wayne King
Wayne King 2011 年 12 月 13 日
Walter, you should keep your posts in the thread. Why are you trying this?
x = input('Enter a integer: ');
for x=1:x,
k=randperm(x);
fprintf('%d\n',k)
end
Do not do for x=1:x, that is not what I showed you, read the help for randperm() it gives you a random permutation up to x
Walter Diolosa
Walter Diolosa 2011 年 12 月 13 日
Thanks!
Happy Holidays.

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

その他の回答 (1 件)

Wayne King
Wayne King 2011 年 12 月 13 日

0 投票

x = input('Enter a integer:\n');
y = randperm(x);
fprintf('%d\n',y);
Why are you trying to put it in to a for loop?

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by