在范围内生成一个随机数?(复制)

时间:2022-11-25 19:42:46

Possible Duplicate:
Generating Random Numbers in Objective-C

可能的重复:在Objective-C中生成随机数。

How do I generate a random number which is within a range?

如何生成一个范围内的随机数?

6 个解决方案

#1


54  

This is actually a bit harder to get really correct than most people realize:

这实际上比大多数人意识到的要正确得多:

int rand_lim(int limit) {
/* return a random number between 0 and limit inclusive.
 */

    int divisor = RAND_MAX/(limit+1);
    int retval;

    do { 
        retval = rand() / divisor;
    } while (retval > limit);

    return retval;
}

Attempts that just use % (or, equivalently, /) to get the numbers in a range almost inevitably introduce skew (i.e., some numbers will be generated more often than others).

仅仅使用%(或者,等价地,/)来得到一个范围内的数字的尝试几乎不可避免地引入了歪斜(也就是)。,一些数字将会比其他数字更频繁地产生。

As to why using % produces skewed results: unless the range you want is a divisor of RAND_MAX, skew is inevitable. If you start with small numbers, it's pretty easy to see why. Consider taking 10 pieces of candy and trying to divide it evenly between three children. Clearly it can't be done -- if you hand out all the candy, the closest you can get is for two kids to get three pieces of candy, and one of them getting four.

至于为什么使用%会产生扭曲的结果:除非你想要的范围是RAND_MAX的除数,否则倾斜是不可避免的。如果从小数开始,很容易看出原因。考虑吃10块糖果,试着把它平均分给3个孩子。显然这是不可能的——如果你把所有的糖果都分发出去,你能得到的最接近的是两个孩子得到3块糖,其中一个得到4块。

There's only one way for all the kids to get the same number of pieces of candy: make sure you don't hand out the last piece of candy at all.

对于所有的孩子来说,只有一种方法可以得到同样数量的糖果:确保你不会分发最后一块糖果。

To relate this to the code above, let's start by numbering the candies from 1 to 10 and the kids from 1 to 3. The initial division says since there are three kids, our divisor is three. We then pull a random candy from the bucket, look at its number and divide by three and hand it to that kid -- but if the result is greater than 3 (i.e. we've picked out candy number 10) we just don't hand it out at all -- we discard it and pick out another candy.

为了将其与上面的代码联系起来,让我们从1到10的糖果和从1到3的孩子开始。最初的除法说,因为有三个孩子,我们的除数是3。然后我们将随机从桶糖果,看它的数量和除以三,交给孩子,但如果结果是大于3(即我们已经挑出糖果10号)我们只是不出来——我们抛弃它,挑出另一个糖果。

Of course, if you're using a modern implementation of C++ (i.e., one that supports C++11 or newer), you should usually use one the distribution classes from the standard library. The code above corresponds most closely with std::uniform_int_distribution, but the standard library also includes uniform_real_distribution as well as classes for a number of non-uniform distributions (Bernoulli, Poisson, normal, maybe a couple others I don't remember at the moment).

当然,如果您使用的是c++的现代实现(例如:一个支持c++ 11或更新版本的程序,通常应该使用一个来自标准库的分发类。上面的代码最接近std::uniform_int_distribution,但是标准库还包括了uniform_real_distribution和一些非均匀分布的类(Bernoulli, Poisson, normal,可能还有一些我现在不记得的)。

#2


7  

int rand_range(int min_n, int max_n)
{
    return rand() % (max_n - min_n + 1) + min_n;
}

For fractions:

分数:

double rand_range(double min_n, double max_n)
{
    return (double)rand()/RAND_MAX * (max_n - min_n) + min_n;
}

#3


2  

http://www.cprogramming.com/tutorial/random.html

http://www.cprogramming.com/tutorial/random.html

The second example shows how to generate by range

第二个示例演示如何按范围生成。

#4


2  

For an integer value in the range [min,max):

对于范围内的整数值[min,max]:

double scale = (double) (max - min) / RAND_MAX;
int val = min + floor(rand() * scale) 

#5


-1  

I wrote this specifically in Obj-C for an iPhone project:

我在objc - c中专门为iPhone项目写了这篇文章:

- (int) intInRangeMinimum:(int)min andMaximum:(int)max {
    if (min > max) { return -1; }
    int adjustedMax = (max + 1) - min; // arc4random returns within the set {min, (max - 1)}
    int random = arc4random() % adjustedMax;
    int result = random + min;
    return result;
}

To use:

使用方法:

int newNumber = [aClass intInRangeMinimum:1 andMaximum:100]; 

Add salt to taste

加盐调味

#6


-2  

+(NSInteger)randomNumberWithMin:(NSInteger)min WithMax:(NSInteger)max {
    if (min>max) {
        int tempMax=max;
        max=min;
        min=tempMax;
    }
    int randomy=arc4random() % (max-min+1);
    randomy=randomy+min;
    return randomy;
}

I use this method in a random number related class I made. Works well for my non-demanding needs, but may well be biased in some way.

我在我做的一个随机数类中使用这个方法。对我的非要求的需求很有效,但很可能在某种程度上有偏见。

#1


54  

This is actually a bit harder to get really correct than most people realize:

这实际上比大多数人意识到的要正确得多:

int rand_lim(int limit) {
/* return a random number between 0 and limit inclusive.
 */

    int divisor = RAND_MAX/(limit+1);
    int retval;

    do { 
        retval = rand() / divisor;
    } while (retval > limit);

    return retval;
}

Attempts that just use % (or, equivalently, /) to get the numbers in a range almost inevitably introduce skew (i.e., some numbers will be generated more often than others).

仅仅使用%(或者,等价地,/)来得到一个范围内的数字的尝试几乎不可避免地引入了歪斜(也就是)。,一些数字将会比其他数字更频繁地产生。

As to why using % produces skewed results: unless the range you want is a divisor of RAND_MAX, skew is inevitable. If you start with small numbers, it's pretty easy to see why. Consider taking 10 pieces of candy and trying to divide it evenly between three children. Clearly it can't be done -- if you hand out all the candy, the closest you can get is for two kids to get three pieces of candy, and one of them getting four.

至于为什么使用%会产生扭曲的结果:除非你想要的范围是RAND_MAX的除数,否则倾斜是不可避免的。如果从小数开始,很容易看出原因。考虑吃10块糖果,试着把它平均分给3个孩子。显然这是不可能的——如果你把所有的糖果都分发出去,你能得到的最接近的是两个孩子得到3块糖,其中一个得到4块。

There's only one way for all the kids to get the same number of pieces of candy: make sure you don't hand out the last piece of candy at all.

对于所有的孩子来说,只有一种方法可以得到同样数量的糖果:确保你不会分发最后一块糖果。

To relate this to the code above, let's start by numbering the candies from 1 to 10 and the kids from 1 to 3. The initial division says since there are three kids, our divisor is three. We then pull a random candy from the bucket, look at its number and divide by three and hand it to that kid -- but if the result is greater than 3 (i.e. we've picked out candy number 10) we just don't hand it out at all -- we discard it and pick out another candy.

为了将其与上面的代码联系起来,让我们从1到10的糖果和从1到3的孩子开始。最初的除法说,因为有三个孩子,我们的除数是3。然后我们将随机从桶糖果,看它的数量和除以三,交给孩子,但如果结果是大于3(即我们已经挑出糖果10号)我们只是不出来——我们抛弃它,挑出另一个糖果。

Of course, if you're using a modern implementation of C++ (i.e., one that supports C++11 or newer), you should usually use one the distribution classes from the standard library. The code above corresponds most closely with std::uniform_int_distribution, but the standard library also includes uniform_real_distribution as well as classes for a number of non-uniform distributions (Bernoulli, Poisson, normal, maybe a couple others I don't remember at the moment).

当然,如果您使用的是c++的现代实现(例如:一个支持c++ 11或更新版本的程序,通常应该使用一个来自标准库的分发类。上面的代码最接近std::uniform_int_distribution,但是标准库还包括了uniform_real_distribution和一些非均匀分布的类(Bernoulli, Poisson, normal,可能还有一些我现在不记得的)。

#2


7  

int rand_range(int min_n, int max_n)
{
    return rand() % (max_n - min_n + 1) + min_n;
}

For fractions:

分数:

double rand_range(double min_n, double max_n)
{
    return (double)rand()/RAND_MAX * (max_n - min_n) + min_n;
}

#3


2  

http://www.cprogramming.com/tutorial/random.html

http://www.cprogramming.com/tutorial/random.html

The second example shows how to generate by range

第二个示例演示如何按范围生成。

#4


2  

For an integer value in the range [min,max):

对于范围内的整数值[min,max]:

double scale = (double) (max - min) / RAND_MAX;
int val = min + floor(rand() * scale) 

#5


-1  

I wrote this specifically in Obj-C for an iPhone project:

我在objc - c中专门为iPhone项目写了这篇文章:

- (int) intInRangeMinimum:(int)min andMaximum:(int)max {
    if (min > max) { return -1; }
    int adjustedMax = (max + 1) - min; // arc4random returns within the set {min, (max - 1)}
    int random = arc4random() % adjustedMax;
    int result = random + min;
    return result;
}

To use:

使用方法:

int newNumber = [aClass intInRangeMinimum:1 andMaximum:100]; 

Add salt to taste

加盐调味

#6


-2  

+(NSInteger)randomNumberWithMin:(NSInteger)min WithMax:(NSInteger)max {
    if (min>max) {
        int tempMax=max;
        max=min;
        min=tempMax;
    }
    int randomy=arc4random() % (max-min+1);
    randomy=randomy+min;
    return randomy;
}

I use this method in a random number related class I made. Works well for my non-demanding needs, but may well be biased in some way.

我在我做的一个随机数类中使用这个方法。对我的非要求的需求很有效,但很可能在某种程度上有偏见。