Generate random integer

조회 수: 4 (최근 30일)
Nurul
Nurul 2011년 3월 22일
Hi,
Can anyone help me to generate several strings of random integer ? In each string, there must be 5 integers generated which the number are in between 1 and 484. let say i wanna have 5 strings of the number. below is the example of the matrix that i need.
random number = [ 4 56 32 75 103; 47 84 302 400 27; 96 187 255 33 14; 387 293 135 34 73; 165 243 93 142 65 ]
Thanks in advanced.
nurul

채택된 답변

Walter Roberson
Walter Roberson 2011년 3월 22일
ceil(484 * rand(5,5))
Note that this has the possibility of generating the same number multiple times. There are slightly different techniques if you require them to be unique along rows or columns.
  댓글 수: 2
Nurul
Nurul 2011년 3월 22일
thanks for your answer as this is what i need for this time. But, what are the technique if i want the number to be unique in each rows and column?
Walter Roberson
Walter Roberson 2011년 3월 22일
The technique that is often used to satisfy uniqueness is to use randperm() to select the elements. Roger Stafford has a much more efficient technique, though; unfortunately I don't understand his algorithm. See for example http://www.mathworks.com/matlabcentral/newsreader/view_thread/149228
I do not recall at the moment the techniques for ensuring that values are unique in both their row and column and yet allowing the value to occur in a different place in the matrix. If the value has to be unique throughout the entire matrix then you just do it like the row or column case and then reshape the result.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sahan
Sahan 2013년 3월 19일
you can use randint function
test = randint(5,5,[1 484])
  댓글 수: 1
Walter Roberson
Walter Roberson 2013년 3월 19일
Note that randint() is part of the Communications Toolbox, and will be removed in the future; randi() is the recommended replacement.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by