Generate random integer
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
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
2013-3-19
you can use randint function
test = randint(5,5,[1 484])
1 个评论
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 Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!