- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Link Copied
1 Reply
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
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

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page