matlab怎么生成指定范圍的隨機數呢?一起來了解一下吧
文章源自四五設計網-http://www.wasochina.com/41281.html
打開matlab文章源自四五設計網-http://www.wasochina.com/41281.html
文章源自四五設計網-http://www.wasochina.com/41281.html
在命令行窗口輸入help rand后按下回車鍵,可以看到有關rand()函數相關介紹文章源自四五設計網-http://www.wasochina.com/41281.html
文章源自四五設計網-http://www.wasochina.com/41281.html
然后輸入 round(rand(1,3)*100),按下回車鍵就生成了3個0-100的隨機整數,如圖所示文章源自四五設計網-http://www.wasochina.com/41281.html
文章源自四五設計網-http://www.wasochina.com/41281.html
輸入round(rand(1,6)*5),按回車鍵就生成了6個0-5的隨機整數,要注意的是,rand函數會出現重復的數據,如果不想要整數的話,去掉round即可文章源自四五設計網-http://www.wasochina.com/41281.html
文章源自四五設計網-http://www.wasochina.com/41281.html
方法2
在命令行窗口輸入help randperm后按下回車鍵,可以看到有關randperm()函數相關介紹文章源自四五設計網-http://www.wasochina.com/41281.html
輸入randperm(3),可以產生3個不會重復的隨機數
輸入randperm(12,5),可以產生5個不會重復的1-10的隨機數
需要注意的是,后面的數字不能大于前面的數字,否則會報錯


評論