Generate Random integer and use as array index

Hi,

I have a problem with finding a solution for generating a random integer number within a defined range. The range should be the size of a matrix or array. Let’s say that I have an matrix with 500 rows. Then I would like to create a generator that generates a random number between 0 and 499. I have looked at mtRandom() function, but it’s not sufficient for this kind of task since it’s only generating numbers between 0-1.

Any suggestions?

Try multiplying the mtRandon result by your number of rows. Take the integer part, and you have an index value…

Hope that Helps.

Snoopy

The solution is very simple:

YourNumber = Round( mtRandom() * 499 );
4 Likes

The numbers 0 and 499 have a 50% less chance being picked than the others.

To address that you should use:

YourNumber = floor( mtRandom() * 500 );
2 Likes