Given a function that randomly generates integers 1 through n, a function (M > N) that can randomly generate integers 1 to M is written.
Set n = 5, M = 7
The key is to have the same probability of generating 1 to 7 of the number. Call N-time given function, generate n 1 to 5
, select the maximum number of positions until the last one is left. Such as:
Initial 7 numbers [1, 2, 3, 4, 5, 6, 7].
(1) 7 random numbers of 1 to 5, such as [5, 3, 1, 4, 2, 5, 5];
(2) The maximum number (5 at this time) appears in the 1,6,7. So select [1,6,7] (the next step is to have the three
Number, until one of the remaining ones is available;
(3) 3 random numbers of 1 to 5, such as [2,4,1];
(4) The maximum number (4 at this point) appears at 2, so select [6]
At this point 6 is the number of random numbers that occur between 1-7.
The code is as follows:
int getrandm (int m)
{
int rands[m + 1], Index[m + 1], result[m + 1];
int I, count;
for (i = 1; I <= m i++)
rands[i] = Getrandn ();
Count = m;
Count = Indexofmaxnumber (Rands, index, count);
for (i = 1; I <= count; i++)
result[i] = index[i];
Do {for
(i = 1; I <= count; i++)
rands[i] = Getrandn ();
Count = Indexofmaxnumber (Rands, index, count);
for (i = 1; I <= count; i++)
result[i] = result[index[i]];, while
(Count > 1);
return result[1];
The random functions that produce 1 to n are as follows:
int Getrandn ()
{
// Srand ((unsigned) time (NULL));/* Random seed */return
rand ()% N + 1;
}
The function of finding the maximum number position in a sequence of random numbers is as follows:
int indexofmaxnumber (int rands[], int index[], int count)
{
int i, Max, Countmax;
for (max = rands[1], i = 2; I <= count; i++)
if (Rands[i] > Max)
max = rands[i];
for (Countmax = 0, i = 1; I <= count; i++)
if (max = = Rands[i])
Index[++countmax] = i;
return countmax;
}
The test code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
int Getrandn ();
int indexofmaxnumber (int rands[], int index[], int count);
int getrandm (int m);
int main (void)
{
srand ((unsigned) time (NULL))/* Random seed */
/n = rand ()% (Y-x + 1) + X;/*n to x~y between random numbers */
int i;
for (i = 0; i < i++)
printf ("%d", GETRANDM (7));
printf ("\ n");
return 0;
}
Output:
The following is the implementation of Java code:
Import Java.util.Random;
public class Randmfromrandn {private static Random Rand = new Random ();
private final int n;
public Randmfromrandn (int n) {THIS.N = n;
public int Getrandn () {return Rand.nextint (n) + 1;
public int indexofmaxnumber (int[] rands, int[] index, int count) {int Max, Countmax;
max = rands[1];
for (int i = 2; I <= count; i++) if (Rands[i] > max) max = rands[i];
Countmax = 0;
for (int i = 1; I <= count; i++) if (rands[i] = max) Index[++countmax] = i;
return Countmax;
public int getrandm (int m) {int[] Rands = new Int[m + 1];
int[] Index = new int[m + 1];
Int[] result = new Int[m + 1];
int count;
for (int i = 1; I <= m i++) rands[i] = Getrandn ();
Count = m;
Count = Indexofmaxnumber (Rands, index, count);
for (int i = 1; I <= count; i++) result[i] = Index[i];
do {for (int i = 1; I <= count; i++) Rands[i] = Getrandn ();
Count = Indexofmaxnumber (Rands, index, count);
for (int i = 1; I <= count; i++) result[i] = result[index[i]];
while (Count > 1);
return result[1];
public static void Main (string[] args) {randmfromrandn nm = new Randmfromrandn (5);
for (int i = 1; I <= i++) System.out.print (NM.GETRANDM (7) + "");
System.out.println (); }
}
Output: