如何从rand()获取特定的数字范围?

时间:2023-02-08 22:51:12
srand(time(null));

printf("%d", rand());

Gives a high-range random number (0-32000ish), but I only need about 0-63 or 0-127, though I'm not sure how to go about it. Any help?

给出一个高范围随机数(0-32000),但我只需要0-63或0-127,尽管我不知道该怎么做。任何帮助吗?

15 个解决方案

#1


37  

rand() % (max_number + 1 - minimum_number) + minimum_number

So, for 0-65:

因此,对于0 - 65:

rand() % (65 + 1 - 0) + 0

(obviously you can leave the 0 off, but it's there for completeness).

(很显然,你可以把0去掉,但它是完整的)。

Note that this will bias the randomness slightly, but probably not anything to be concerned about if you're not doing something particularly sensitive.

请注意,这将略微偏向随机性,但如果您做的事情不是特别敏感,那么可能不需要担心任何事情。

#2


14  

check here

检查在这里

http://c-faq.com/lib/randrange.html

http://c-faq.com/lib/randrange.html

For any of these techniques, it's straightforward to shift the range, if necessary; numbers in the range [M, N] could be generated with something like

对于这些技术中的任何一种,如果有必要,可以直接改变范围;范围内的数字[M, N]可以用类似的东西生成

M + rand() / (RAND_MAX / (N - M + 1) + 1)

#3


5  

Taking the modulo of the result, as the other posters have asserted will give you something that's nearly random, but not perfectly so.

根据结果的模度,正如其他海报所宣称的那样,你会得到一些几乎是随机的东西,但不是完美的。

Consider this extreme example, suppose you wanted to simulate a coin toss, returning either 0 or 1. You might do this:

考虑这个极端的例子,假设您想要模拟抛硬币,返回0或1。你可能会这样做:

isHeads = ( rand() % 2 ) == 1;

Looks harmless enough, right? Suppose that RAND_MAX is only 3. It's much higher of course, but the point here is that there's a bias when you use a modulus that doesn't evenly divide RAND_MAX. If you want high quality random numbers, you're going to have a problem.

看起来无害,对吧?假设RAND_MAX只有3。它当然要高得多,但这里的重点是当你使用一个不平均除RAND_MAX的模数时存在偏差。如果你想要高质量的随机数,你就会遇到问题。

Consider my example. The possible outcomes are:

考虑我的例子。可能的结果是:

rand()  freq. rand() % 2
0       1/3   0
1       1/3   1
2       1/3   0

Hence, "tails" will happen twice as often as "heads"!

因此,“反面”发生的频率是“正面”的两倍!

Mr. Atwood discusses this matter in this Coding Horror Article

阿特伍德先生在这篇编码恐怖文章中讨论了这个问题

#4


5  

As others have noted, simply using a modulus will skew the probabilities for individual numbers so that smaller numbers are preferred.

正如其他人所注意到的,仅仅使用一个模数就会使单个数字的概率发生偏差,因此更倾向于使用较小的数字。

A very ingenious and good solution to that problem is used in Java's java.util.Random class:

Java的Java .util中使用了一个非常巧妙的、很好的解决方案。随机类:

public int nextInt(int n) {
    if (n <= 0)
        throw new IllegalArgumentException("n must be positive");

    if ((n & -n) == n)  // i.e., n is a power of 2
        return (int)((n * (long)next(31)) >> 31);

    int bits, val;
    do {
        bits = next(31);
        val = bits % n;
    } while (bits - val + (n-1) < 0);
    return val;
}

It took me a while to understand why it works and I leave that as an exercise for the reader but it's a pretty concise solution which will ensure that numbers have equal probabilities.

我花了一段时间才明白它为什么有效,我把它留给读者作为练习,但它是一个非常简洁的解决方案,可以确保数字有相同的概率。

The important part in that piece of code is the condition for the while loop, which rejects numbers that fall in the range of numbers which otherwise would result in an uneven distribution.

这段代码中最重要的部分是while循环的条件,它拒绝在数字范围内的数字,否则会导致不均匀的分布。

#5


5  

You can use this:

您可以使用:

int random(int min, int max){
   return min + rand() / (RAND_MAX / (max - min + 1) + 1);
}

From the:

从:

comp.lang.c FAQ list · Question 13.16

Q: How can I get random integers in a certain range?

A: The obvious way,

rand() % N        /* POOR */

(which tries to return numbers from 0 to N-1) is poor, because the low-order bits of many random number generators are distressingly non-random. (See question 13.18.) A better method is something like

(它试图从0返回到N-1)很糟糕,因为许多随机数生成器的低阶位都是非随机的。(见13.18的问题。)更好的方法是

(int)((double)rand() / ((double)RAND_MAX + 1) * N)

If you'd rather not use floating point, another method is

如果您不想使用浮点数,另一个方法是

rand() / (RAND_MAX / N + 1)

If you just need to do something with probability 1/N, you could use

如果你只需要做一些概率为1/N的事情,你可以用

if(rand() < (RAND_MAX+1u) / N)

All these methods obviously require knowing RAND_MAX (which ANSI #defines in ), and assume that N is much less than RAND_MAX. When N is close to RAND_MAX, and if the range of the random number generator is not a multiple of N (i.e. if (RAND_MAX+1) % N != 0), all of these methods break down: some outputs occur more often than others. (Using floating point does not help; the problem is that rand returns RAND_MAX+1 distinct values, which cannot always be evenly divvied up into N buckets.) If this is a problem, about the only thing you can do is to call rand multiple times, discarding certain values:

显然,所有这些方法都需要知道RAND_MAX (ANSI #在其中定义),并假定N比RAND_MAX要小得多。当N接近RAND_MAX时,如果随机数生成器的范围不是N的倍数(例如(RAND_MAX+1) % N !(使用浮点数没有帮助;问题是rand返回了RAND_MAX+1个不同的值,这些值不能均匀地分配到N个桶中。如果这是一个问题,你唯一能做的就是多次调用rand,丢弃某些值:

unsigned int x = (RAND_MAX + 1u) / N;
unsigned int y = x * N;
unsigned int r;
do {
  r = rand();
} while(r >= y);
return r / x;

For any of these techniques, it's straightforward to shift the range, if necessary; numbers in the range [M, N] could be generated with something like

对于这些技术中的任何一种,如果有必要,可以直接改变范围;范围内的数字[M, N]可以用类似的东西生成

M + rand() / (RAND_MAX / (N - M + 1) + 1)

(Note, by the way, that RAND_MAX is a constant telling you what the fixed range of the C library rand function is. You cannot set RAND_MAX to some other value, and there is no way of requesting that rand return numbers in some other range.)

(顺便说一下,RAND_MAX是一个常数,告诉你C库rand函数的固定范围是什么。不能将RAND_MAX设置为其他值,也不能在其他范围内请求rand返回值)。

If you're starting with a random number generator which returns floating-point values between 0 and 1 (such as the last version of PMrand alluded to in question 13.15, or drand48 in question 13.21), all you have to do to get integers from 0 to N-1 is multiply the output of that generator by N:

如果你从一个随机数发生器返回浮点值在0和1之间(如PMrand提到的最后版本13.15,13.21或drand48问题),你所要做的从0到N - 1的整数,发电机的输出乘以N:

(int)(drand48() * N)

Additional links

额外的链接

References: K&R2 Sec. 7.8.7 p. 168 PCS Sec. 11 p. 172

参考文献:K&R2 . 7.8.7页168页11页172页

Quote from: http://c-faq.com/lib/randrange.html

引用:http://c-faq.com/lib/randrange.html

#6


3  

double scale = 1.0 / ((double) RAND_MAX + 1.0);
int min, max;
...
rval = (int)(rand() * scale * (max - min + 1) + min);

#7


2  

If you don't overly care about the 'randomness' of the low-order bits, just rand() % HI_VAL.

如果您不太关心低阶位的“随机性”,只需rand() % HI_VAL。

Also:

另外:

(double)rand() / (double)RAND_MAX;  // lazy way to get [0.0, 1.0)

#8


2  

The naive way to do it is:

天真的做法是:

int myRand = rand() % 66; // for 0-65

This will likely be a very slightly non-uniform distribution (depending on your maximum value), but it's pretty close.

这可能是一个非常不均匀的分布(取决于你的最大值),但是非常接近。

To explain why it's not quite uniform, consider this very simplified example:
Suppose RAND_MAX is 4 and you want a number from 0-2. The possible values you can get are shown in this table:

要解释为什么它不是完全一致的,请考虑这个非常简化的示例:假定RAND_MAX为4,并且需要0-2中的一个数字。您可以得到的可能值如下表所示:

rand()   |  rand() % 3
---------+------------
0        |  0
1        |  1
2        |  2
3        |  0

See the problem? If your maximum value is not an even divisor of RAND_MAX, you'll be more likely to choose small values. However, since RAND_MAX is generally 32767, the bias is likely to be small enough to get away with for most purposes.

看到这个问题吗?如果您的最大值不是RAND_MAX的偶数因子,那么您将更有可能选择较小的值。然而,由于RAND_MAX一般是32767,所以偏差可能很小,大多数情况下都可以避免。

There are various ways to get around this problem; see here for an explanation of how Java's Random handles it.

有很多方法可以解决这个问题;有关Java的Random如何处理它的解释,请参见这里。

#9


2  

Updated to not use a #define

更新为不使用#define

double RAND(double min, double max)
{
    return (double)rand()/(double)RAND_MAX * (max - min) + min;
}

#10


1  

rand() will return numbers between 0 and RAND_MAX, which is at least 32767.

rand()将返回0到RAND_MAX之间的数字,这至少是32767。

If you want to get a number within a range, you can just use modulo.

如果你想在一个范围内得到一个数字,你可以使用modulo。

int value = rand() % 66; // 0-65

For more accuracy, check out this article. It discusses why modulo is not necessarily good (bad distributions, particularly on the high end), and provides various options.

要获得更准确的信息,请参阅本文。它讨论了为什么模块化并不一定是好的(不好的分布,尤其是在高端),并提供了各种选择。

#11


0  

I think the following does it semi right. It's been awhile since I've touched C. The idea is to use division since modulus doesn't always give random results. I added 1 to RAND_MAX since there are that many possible values coming from rand including 0. And since the range is also 0 inclusive, I added 1 there too. I think the math is arranged correctly avoid integer math problems.

我认为下面的方法是正确的。我接触c已经有一段时间了,这个想法是用除法,因为模量并不总是给出随机的结果。我向RAND_MAX添加了1,因为有很多可能的值来自rand,包括0。因为范围也是0,所以我在这里加了1。我认为数学是正确安排的避免整数数学问题。

#define MK_DIVISOR(max) ((int)((unsigned int)RAND_MAX+1/(max+1)))

num = rand()/MK_DIVISOR(65);

#12


0  

if you care about the quality of your random numbers don't use rand()

如果你关心随机数的质量不要使用rand()

use some other prng like http://en.wikipedia.org/wiki/Mersenne_twister or one of the other high quality prng's out there

使用其他一些prng,比如http://en.wikipedia.org/wiki/Mersenne_twister或者其他高质量的prng

then just go with the modulus.

然后用模数。

#13


0  

Just to add some extra detail to the existing answers.

在现有的答案中添加一些额外的细节。

The mod % operation will always perform a complete division and therefore yield a remainder less than the divisor.

mod %操作将始终执行完整的除法,因此产生的余数小于除数。

x % y = x - (y * floor((x/y)))

x % y = x - (y *地板(x/y))

An example of a random range finding function with comments:

带有注释的随机范围查找函数示例:

uint32_t rand_range(uint32_t n, uint32_t m) {
    // size of range, inclusive
    const uint32_t length_of_range = m - n + 1;

    // add n so that we don't return a number below our range
    return (uint32_t)(rand() % length_of_range + n);
}

Another interesting property as per the above:

另一个有趣的性质是:

x % y = x, if x < y

x % y = x,如果x < y

const uint32_t value = rand_range(1, RAND_MAX); // results in rand() % RAND_MAX + 1
// TRUE for all x = RAND_MAX, where x is the result of rand()
assert(value == RAND_MAX);
result of rand()

#14


0  

2 cents (ok 4 cents):

n = rand()
x = result
l = limit

n/RAND_MAX = x/l

Refactor:

(l/1)*(n/RAND_MAX) = (x/l)*(l/1)

Gives:

x = l*n/RAND_MAX

int randn(int limit)

{

    return limit*rand()/RAND_MAX;

}

int i;

for (i = 0; i < 100; i++) { 

    printf("%d ", randn(10)); 
    if (!(i % 16)) printf("\n"); 

}

> test
0
5 1 8 5 4 3 8 8 7 1 8 7 5 3 0 0
3 1 1 9 4 1 0 0 3 5 5 6 6 1 6 4
3 0 6 7 8 5 3 8 7 9 9 5 1 4 2 8
2 7 8 9 9 6 3 2 2 8 0 3 0 6 0 0
9 2 2 5 6 8 7 4 2 7 4 4 9 7 1 5
3 7 6 5 3 1 2 4 8 5 9 7 3 1 6 4
0 6 5

#15


-1  

Or you can use this:

或者你可以用这个:

rand() / RAND_MAX * 65

But I'm not sure if it's the most random or fastest of all the answers here.

但我不确定这是最随机的还是最快的答案。

#1


37  

rand() % (max_number + 1 - minimum_number) + minimum_number

So, for 0-65:

因此,对于0 - 65:

rand() % (65 + 1 - 0) + 0

(obviously you can leave the 0 off, but it's there for completeness).

(很显然,你可以把0去掉,但它是完整的)。

Note that this will bias the randomness slightly, but probably not anything to be concerned about if you're not doing something particularly sensitive.

请注意,这将略微偏向随机性,但如果您做的事情不是特别敏感,那么可能不需要担心任何事情。

#2


14  

check here

检查在这里

http://c-faq.com/lib/randrange.html

http://c-faq.com/lib/randrange.html

For any of these techniques, it's straightforward to shift the range, if necessary; numbers in the range [M, N] could be generated with something like

对于这些技术中的任何一种,如果有必要,可以直接改变范围;范围内的数字[M, N]可以用类似的东西生成

M + rand() / (RAND_MAX / (N - M + 1) + 1)

#3


5  

Taking the modulo of the result, as the other posters have asserted will give you something that's nearly random, but not perfectly so.

根据结果的模度,正如其他海报所宣称的那样,你会得到一些几乎是随机的东西,但不是完美的。

Consider this extreme example, suppose you wanted to simulate a coin toss, returning either 0 or 1. You might do this:

考虑这个极端的例子,假设您想要模拟抛硬币,返回0或1。你可能会这样做:

isHeads = ( rand() % 2 ) == 1;

Looks harmless enough, right? Suppose that RAND_MAX is only 3. It's much higher of course, but the point here is that there's a bias when you use a modulus that doesn't evenly divide RAND_MAX. If you want high quality random numbers, you're going to have a problem.

看起来无害,对吧?假设RAND_MAX只有3。它当然要高得多,但这里的重点是当你使用一个不平均除RAND_MAX的模数时存在偏差。如果你想要高质量的随机数,你就会遇到问题。

Consider my example. The possible outcomes are:

考虑我的例子。可能的结果是:

rand()  freq. rand() % 2
0       1/3   0
1       1/3   1
2       1/3   0

Hence, "tails" will happen twice as often as "heads"!

因此,“反面”发生的频率是“正面”的两倍!

Mr. Atwood discusses this matter in this Coding Horror Article

阿特伍德先生在这篇编码恐怖文章中讨论了这个问题

#4


5  

As others have noted, simply using a modulus will skew the probabilities for individual numbers so that smaller numbers are preferred.

正如其他人所注意到的,仅仅使用一个模数就会使单个数字的概率发生偏差,因此更倾向于使用较小的数字。

A very ingenious and good solution to that problem is used in Java's java.util.Random class:

Java的Java .util中使用了一个非常巧妙的、很好的解决方案。随机类:

public int nextInt(int n) {
    if (n <= 0)
        throw new IllegalArgumentException("n must be positive");

    if ((n & -n) == n)  // i.e., n is a power of 2
        return (int)((n * (long)next(31)) >> 31);

    int bits, val;
    do {
        bits = next(31);
        val = bits % n;
    } while (bits - val + (n-1) < 0);
    return val;
}

It took me a while to understand why it works and I leave that as an exercise for the reader but it's a pretty concise solution which will ensure that numbers have equal probabilities.

我花了一段时间才明白它为什么有效,我把它留给读者作为练习,但它是一个非常简洁的解决方案,可以确保数字有相同的概率。

The important part in that piece of code is the condition for the while loop, which rejects numbers that fall in the range of numbers which otherwise would result in an uneven distribution.

这段代码中最重要的部分是while循环的条件,它拒绝在数字范围内的数字,否则会导致不均匀的分布。

#5


5  

You can use this:

您可以使用:

int random(int min, int max){
   return min + rand() / (RAND_MAX / (max - min + 1) + 1);
}

From the:

从:

comp.lang.c FAQ list · Question 13.16

Q: How can I get random integers in a certain range?

A: The obvious way,

rand() % N        /* POOR */

(which tries to return numbers from 0 to N-1) is poor, because the low-order bits of many random number generators are distressingly non-random. (See question 13.18.) A better method is something like

(它试图从0返回到N-1)很糟糕,因为许多随机数生成器的低阶位都是非随机的。(见13.18的问题。)更好的方法是

(int)((double)rand() / ((double)RAND_MAX + 1) * N)

If you'd rather not use floating point, another method is

如果您不想使用浮点数,另一个方法是

rand() / (RAND_MAX / N + 1)

If you just need to do something with probability 1/N, you could use

如果你只需要做一些概率为1/N的事情,你可以用

if(rand() < (RAND_MAX+1u) / N)

All these methods obviously require knowing RAND_MAX (which ANSI #defines in ), and assume that N is much less than RAND_MAX. When N is close to RAND_MAX, and if the range of the random number generator is not a multiple of N (i.e. if (RAND_MAX+1) % N != 0), all of these methods break down: some outputs occur more often than others. (Using floating point does not help; the problem is that rand returns RAND_MAX+1 distinct values, which cannot always be evenly divvied up into N buckets.) If this is a problem, about the only thing you can do is to call rand multiple times, discarding certain values:

显然,所有这些方法都需要知道RAND_MAX (ANSI #在其中定义),并假定N比RAND_MAX要小得多。当N接近RAND_MAX时,如果随机数生成器的范围不是N的倍数(例如(RAND_MAX+1) % N !(使用浮点数没有帮助;问题是rand返回了RAND_MAX+1个不同的值,这些值不能均匀地分配到N个桶中。如果这是一个问题,你唯一能做的就是多次调用rand,丢弃某些值:

unsigned int x = (RAND_MAX + 1u) / N;
unsigned int y = x * N;
unsigned int r;
do {
  r = rand();
} while(r >= y);
return r / x;

For any of these techniques, it's straightforward to shift the range, if necessary; numbers in the range [M, N] could be generated with something like

对于这些技术中的任何一种,如果有必要,可以直接改变范围;范围内的数字[M, N]可以用类似的东西生成

M + rand() / (RAND_MAX / (N - M + 1) + 1)

(Note, by the way, that RAND_MAX is a constant telling you what the fixed range of the C library rand function is. You cannot set RAND_MAX to some other value, and there is no way of requesting that rand return numbers in some other range.)

(顺便说一下,RAND_MAX是一个常数,告诉你C库rand函数的固定范围是什么。不能将RAND_MAX设置为其他值,也不能在其他范围内请求rand返回值)。

If you're starting with a random number generator which returns floating-point values between 0 and 1 (such as the last version of PMrand alluded to in question 13.15, or drand48 in question 13.21), all you have to do to get integers from 0 to N-1 is multiply the output of that generator by N:

如果你从一个随机数发生器返回浮点值在0和1之间(如PMrand提到的最后版本13.15,13.21或drand48问题),你所要做的从0到N - 1的整数,发电机的输出乘以N:

(int)(drand48() * N)

Additional links

额外的链接

References: K&R2 Sec. 7.8.7 p. 168 PCS Sec. 11 p. 172

参考文献:K&R2 . 7.8.7页168页11页172页

Quote from: http://c-faq.com/lib/randrange.html

引用:http://c-faq.com/lib/randrange.html

#6


3  

double scale = 1.0 / ((double) RAND_MAX + 1.0);
int min, max;
...
rval = (int)(rand() * scale * (max - min + 1) + min);

#7


2  

If you don't overly care about the 'randomness' of the low-order bits, just rand() % HI_VAL.

如果您不太关心低阶位的“随机性”,只需rand() % HI_VAL。

Also:

另外:

(double)rand() / (double)RAND_MAX;  // lazy way to get [0.0, 1.0)

#8


2  

The naive way to do it is:

天真的做法是:

int myRand = rand() % 66; // for 0-65

This will likely be a very slightly non-uniform distribution (depending on your maximum value), but it's pretty close.

这可能是一个非常不均匀的分布(取决于你的最大值),但是非常接近。

To explain why it's not quite uniform, consider this very simplified example:
Suppose RAND_MAX is 4 and you want a number from 0-2. The possible values you can get are shown in this table:

要解释为什么它不是完全一致的,请考虑这个非常简化的示例:假定RAND_MAX为4,并且需要0-2中的一个数字。您可以得到的可能值如下表所示:

rand()   |  rand() % 3
---------+------------
0        |  0
1        |  1
2        |  2
3        |  0

See the problem? If your maximum value is not an even divisor of RAND_MAX, you'll be more likely to choose small values. However, since RAND_MAX is generally 32767, the bias is likely to be small enough to get away with for most purposes.

看到这个问题吗?如果您的最大值不是RAND_MAX的偶数因子,那么您将更有可能选择较小的值。然而,由于RAND_MAX一般是32767,所以偏差可能很小,大多数情况下都可以避免。

There are various ways to get around this problem; see here for an explanation of how Java's Random handles it.

有很多方法可以解决这个问题;有关Java的Random如何处理它的解释,请参见这里。

#9


2  

Updated to not use a #define

更新为不使用#define

double RAND(double min, double max)
{
    return (double)rand()/(double)RAND_MAX * (max - min) + min;
}

#10


1  

rand() will return numbers between 0 and RAND_MAX, which is at least 32767.

rand()将返回0到RAND_MAX之间的数字,这至少是32767。

If you want to get a number within a range, you can just use modulo.

如果你想在一个范围内得到一个数字,你可以使用modulo。

int value = rand() % 66; // 0-65

For more accuracy, check out this article. It discusses why modulo is not necessarily good (bad distributions, particularly on the high end), and provides various options.

要获得更准确的信息,请参阅本文。它讨论了为什么模块化并不一定是好的(不好的分布,尤其是在高端),并提供了各种选择。

#11


0  

I think the following does it semi right. It's been awhile since I've touched C. The idea is to use division since modulus doesn't always give random results. I added 1 to RAND_MAX since there are that many possible values coming from rand including 0. And since the range is also 0 inclusive, I added 1 there too. I think the math is arranged correctly avoid integer math problems.

我认为下面的方法是正确的。我接触c已经有一段时间了,这个想法是用除法,因为模量并不总是给出随机的结果。我向RAND_MAX添加了1,因为有很多可能的值来自rand,包括0。因为范围也是0,所以我在这里加了1。我认为数学是正确安排的避免整数数学问题。

#define MK_DIVISOR(max) ((int)((unsigned int)RAND_MAX+1/(max+1)))

num = rand()/MK_DIVISOR(65);

#12


0  

if you care about the quality of your random numbers don't use rand()

如果你关心随机数的质量不要使用rand()

use some other prng like http://en.wikipedia.org/wiki/Mersenne_twister or one of the other high quality prng's out there

使用其他一些prng,比如http://en.wikipedia.org/wiki/Mersenne_twister或者其他高质量的prng

then just go with the modulus.

然后用模数。

#13


0  

Just to add some extra detail to the existing answers.

在现有的答案中添加一些额外的细节。

The mod % operation will always perform a complete division and therefore yield a remainder less than the divisor.

mod %操作将始终执行完整的除法,因此产生的余数小于除数。

x % y = x - (y * floor((x/y)))

x % y = x - (y *地板(x/y))

An example of a random range finding function with comments:

带有注释的随机范围查找函数示例:

uint32_t rand_range(uint32_t n, uint32_t m) {
    // size of range, inclusive
    const uint32_t length_of_range = m - n + 1;

    // add n so that we don't return a number below our range
    return (uint32_t)(rand() % length_of_range + n);
}

Another interesting property as per the above:

另一个有趣的性质是:

x % y = x, if x < y

x % y = x,如果x < y

const uint32_t value = rand_range(1, RAND_MAX); // results in rand() % RAND_MAX + 1
// TRUE for all x = RAND_MAX, where x is the result of rand()
assert(value == RAND_MAX);
result of rand()

#14


0  

2 cents (ok 4 cents):

n = rand()
x = result
l = limit

n/RAND_MAX = x/l

Refactor:

(l/1)*(n/RAND_MAX) = (x/l)*(l/1)

Gives:

x = l*n/RAND_MAX

int randn(int limit)

{

    return limit*rand()/RAND_MAX;

}

int i;

for (i = 0; i < 100; i++) { 

    printf("%d ", randn(10)); 
    if (!(i % 16)) printf("\n"); 

}

> test
0
5 1 8 5 4 3 8 8 7 1 8 7 5 3 0 0
3 1 1 9 4 1 0 0 3 5 5 6 6 1 6 4
3 0 6 7 8 5 3 8 7 9 9 5 1 4 2 8
2 7 8 9 9 6 3 2 2 8 0 3 0 6 0 0
9 2 2 5 6 8 7 4 2 7 4 4 9 7 1 5
3 7 6 5 3 1 2 4 8 5 9 7 3 1 6 4
0 6 5

#15


-1  

Or you can use this:

或者你可以用这个:

rand() / RAND_MAX * 65

But I'm not sure if it's the most random or fastest of all the answers here.

但我不确定这是最随机的还是最快的答案。