MATLAB Answers

Matlab equivalent of rand() in C++

19 views (last 30 days)
Rishi Balasubramanian
Rishi Balasubramanian on 17 Apr 2021 at 14:19
Commented: Rishi Balasubramanian on 17 Apr 2021 at 14:41
Hello,
I am trying to adapt a C++ code to my Matlab project.
In C++ there is this line
random4 = rand() % item // Here the % is modulo in C++
So random is the range of values 'rand' modulo by variable 'item'.
How do I implement this in Matlab. Since I am looking for targeted platform and efficiency, I cannot call the Cpp file in matlab. I am looking for a matlab adaptation.
Any help or suggestions offered are much appreciated. Thank you in advance...
  3 Comments
Rishi Balasubramanian
Rishi Balasubramanian on 17 Apr 2021 at 14:41
Ah, wow, that would have been a crucial error. Thanks for pointing it out man.

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 17 Apr 2021 at 14:38
Edited: Bruno Luong on 17 Apr 2021 at 14:39
random4 = randi(item)-1

More Answers (0)

Community Treasure Hunt

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

Start Hunting!