Software Archive
Read-only legacy content
17061 Discussions

Random number between 1 and N

Intel_C_Intel
Employee
484 Views
Thank you very much for your replies. Indeed, my question is how to generate a set of random numbers, say m random numbers, between 1 and N. Here m and N are integers and m <= N. Also, when the program is excuted each time, these m random numbers should be different.
0 Kudos
1 Reply
Steven_L_Intel1
Employee
484 Views
When you say "m <= N", that implies to me that you don't want any duplicates in the set. (If so, then they're not really random.) You can do this, but the technique differs.

Given a value R from RANDOM_NUMBER, you get an integer between 1 and N by doing
 INT(R * REAL(N)) + 1


If you want no duplicates, then you have to allocate an array of size N in which you record whether or not you've already seen each value. If you have, try again. There are various optimizations you can do on top of this.

Steve
0 Kudos
Reply