how to generate random number
조회 수: 9 (최근 30일)
이전 댓글 표시
Hi,
k = 3;
randomNumbers = randi([1, 2], [1, k])
randomNumbers =
1 1 1
I wrote this, but I need decimal numbers from smallest to largest between 1 and 2. What should I do for it?
댓글 수: 0
답변 (2개)
Dyuman Joshi
2024년 3월 8일
댓글 수: 3
Dyuman Joshi
2024년 3월 8일
Multiply and add/subtract accordingly.
See this example - https://in.mathworks.com/help/matlab/ref/rand.html#buiavoq-9
John D'Errico
2024년 3월 8일
From small to large? Nothing stops you from sorting the numbers after you generate them.
help sort
Hassaan
2024년 3월 8일
k = 3;
randomNumbers = 1 + rand(1, k); % Generate k random numbers between 1 and 2
randomNumbersSorted = sort(randomNumbers); % Sort numbers from smallest to largest
disp(randomNumbersSorted)
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!