Iphone 根据概率随机选择项目
我有一个元素数组。每个元素都附加了一个概率值。假设我有一系列的苹果,红的,黄的,绿的,蓝的,等等Iphone 根据概率随机选择项目,iphone,ios,random-sample,Iphone,Ios,Random Sample,我有一个元素数组。每个元素都附加了一个概率值。假设我有一系列的苹果,红的,黄的,绿的,蓝的,等等 - (Apple *)pickRandomApple { Apple *red = [Apple redApple]; Apple *green = [Apple greenApple]; Apple *yellow = [Apple yellowApple]; Apple *blue = [Apple blueApple]; red.probability
- (Apple *)pickRandomApple
{
Apple *red = [Apple redApple];
Apple *green = [Apple greenApple];
Apple *yellow = [Apple yellowApple];
Apple *blue = [Apple blueApple];
red.probability = 0.23f;
green.probability = 0.85f;
yellow.probability = 0.1f;
blue.probability = 0.5f;
NSArray *array = @[red,green,blue,yellow];
return array[arc4random()%array.count];
}
我想根据概率属性随机选择一个苹果。我怎样才能做到这一点
谢谢 一种可能的解决方案是将项目(概率*精度)次添加到数组中,并在其上使用random 否则,您可以计算概率并定义区域
double max = (red.probability + green.probability + yellow.probability
+ blue.probability) * 100.f;
int random = arc4random()%(int)max;
if(random < red.probability * 100)
return red;
else if(random < (red.probability + blue.probability) * 100)
return blue:
...
double max=(红色概率+绿色概率+黄色概率
+蓝色。概率)*100.f;
int random=arc4random()%(int)max;
if(随机<红色概率*100)
返回红色;
否则如果(随机<(红色概率+蓝色概率)*100)
返回蓝色:
...
等等
还可以为此创建for循环:)
更新
double max=(红色概率+绿色概率+黄色概率
+蓝色。概率)*100.f;
//最大值=(0.23+0.85+0.1+0.5)*100;//=1.68 * 100 = 168
int random=arc4random()%(int)max;
if(随机<红色概率*100)//区域0-23
返回红色;
否则如果(随机<(红色概率+蓝色概率)*100)//区域24-108
返回蓝色:
...
在循环中,您可以保存currentValue变量
double max = (red.probability + green.probability + yellow.probability
+ blue.probability) * 100.f;
// max = (0.23 + 0.85 + 0.1 + 0.5) * 100; // = 1.68 * 100 = 168
int random = arc4random()%(int)max;
int currentValue = 0;
for(Apple *apple in array)
{
currentValue += (int)(apple.probability * 100.f);
if(random <= currentValue)
return apple;
}
double max=(红色概率+绿色概率+黄色概率
+蓝色。概率)*100.f;
//最大值=(0.23+0.85+0.1+0.5)*100;//=1.68 * 100 = 168
int random=arc4random()%(int)max;
int currentValue=0;
用于(苹果*阵列中的苹果)
{
当前值+=(int)(苹果概率*100.f);
如果(random我从你的问题中了解到,你希望生成具有强度的概率,比如绿色比红色更可能出现等等……这个问题与iOS或iPhone开发无关,它是纯粹的编程问题,我认为没有一个解决方案。这是我的解决方案,我想使用通常是简单的解决方案
在发布代码之前,我想解释一下我的想法。这个问题类似于我们在高中学习的概率问题,所以我也会用同样的方法来解决它。我也会用你的数字。所以考虑一下你有一个盒子,里面有23个红球,85个绿球,10个黄球和50个蓝球。所有这些球都是I。n一个方块,你必须从方块中随机选择一个球,因此红色的概率为0.23,绿色的概率为0.85
这是我的彩球盒:
char apples[] = {'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 'b', 'b', 'r', 'r', 'y', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 'b', 'b', 'r', 'r', 'y', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 'b', 'b', 'r', 'r', 'y', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 'b', 'b', 'r', 'r', 'y', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 'b', 'b', 'r', 'r', 'y', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 'b', 'b', 'r', 'r', 'y', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 'b', 'b', 'r', 'r', 'y', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 'b', 'b', 'r', 'r', 'y', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 'b', 'b', 'r', 'r', 'y', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 'b', 'b', 'r', 'r', 'y', 'r', 'g', 'g', 'r', 'g', 'g', 'r', 'g'};
你注意到我在数组中使用一种特殊的分布方式来分布球。好吧,想象一下使用你的数字的方框示例。你有10个黄色的球和85个绿色的球,这意味着我希望在方框中每1个黄色的球旁边看到8个绿色的球,同样的,我也希望看到5个蓝色和2个红色的球。因为绿色是最专业的bable我希望先看到它。因此,您可以看到我的数组具有以下模式:
'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 'b', 'b', 'r', 'r', 'y',
也就是说每1个黄色,8个绿色,5个蓝色,2个红色。因为还有更多的红色和绿色,我把它们添加到了数组的末尾
现在这个过程非常简单,因为这个集合包含168个球,我做了一个循环,运行168次,每次它生成一个从0到167的数字。我使用这个数字作为数组苹果的索引,我看到了我得到的球
int counters[4] = {0, 0, 0, 0};
int x;
for(int i=0; i<168; i++)
{
x = arc4random()%168;
if(apples[x] == 'r') counters[0]++;
else if(apples[x] == 'g') counters[1]++;
else if(apples[x] == 'y') counters[2]++;
else if(apples[x] == 'b') counters[3]++;
}
NSLog(@"Red: %i%%", counters[0]);
NSLog(@"Green: %i%%", counters[1]);
NSLog(@"Yellow: %i%%", counters[2]);
NSLog(@"Blue: %i%%", counters[3]);
你可以随意使用数组来增强效果。这是我的方法,它更通用一些。
请随意提问/编辑
/**
@param probabilitiesArray - array of integers who represent probabilities.
If the first var in the array is 20 and the rest of the array's variables
sum up to 100 - there are 20% probability for first var to be chosen.
*/
-(int) randomNumberWithProbabilities:(NSArray *) probabilitiesArray
{
//Sum up all the variables in the array
int arraySum = 0;
for(int i = 0;i < probabilitiesArray.count;i++)
{
arraySum += [probabilitiesArray[i] intValue];
}
//Random a number from 0 to the sum of the array variables
int randomNumber = arc4random_uniform(arraySum);
//Iterating through the array variable and detect the right slot for the random number
int tempSum = 0;
for(int i = 0;i < probabilitiesArray.count;i++)
{
tempSum += [probabilitiesArray[i] intValue];
if(randomNumber < tempSum)
{
return i;
}
}
return 0;
}
/**
@param ProbabilityArray—表示概率的整数数组。
如果数组中的第一个变量是20,而数组的其余变量
总计100-选择第一个风险值的概率为20%。
*/
-(int)具有概率的随机数:(NSArray*)概率数组
{
//对数组中的所有变量求和
int-arraySum=0;
for(int i=0;i
非常有趣。这是否意味着如果一个元素的概率为100,所有其他元素都将被忽略?如果我有2个以上的元素具有相同的概率,会发生什么?我不也必须将它们随机化吗?对于你的第一条评论:使用“区域”将涵盖所有情况,因为每个元素都有自己的面积,大小不重要。我将用一些解释更新我的答案并添加更多代码。希望意图明确^^^检查一下,可能遗漏了一些+1(不能保证random会给你正确的最小值和最大值,这只是我的第一个想法);)这个答案中的最后一个代码看起来是正确的。@不要使用模(%)来产生随机性,这不是很好。
2013-07-04 19:48:54.557 DOS[798:707] Red: 24%
2013-07-04 19:48:54.560 DOS[798:707] Green: 78%
2013-07-04 19:48:54.562 DOS[798:707] Yellow: 11%
2013-07-04 19:48:54.563 DOS[798:707] Blue: 55%
2013-07-04 19:49:04.899 DOS[811:707] Red: 21%
2013-07-04 19:49:04.901 DOS[811:707] Green: 81%
2013-07-04 19:49:04.905 DOS[811:707] Yellow: 9%
2013-07-04 19:49:04.906 DOS[811:707] Blue: 57%
2013-07-04 19:49:15.243 DOS[824:707] Red: 20%
2013-07-04 19:49:15.246 DOS[824:707] Green: 89%
2013-07-04 19:49:15.246 DOS[824:707] Yellow: 8%
2013-07-04 19:49:15.247 DOS[824:707] Blue: 51%
/**
@param probabilitiesArray - array of integers who represent probabilities.
If the first var in the array is 20 and the rest of the array's variables
sum up to 100 - there are 20% probability for first var to be chosen.
*/
-(int) randomNumberWithProbabilities:(NSArray *) probabilitiesArray
{
//Sum up all the variables in the array
int arraySum = 0;
for(int i = 0;i < probabilitiesArray.count;i++)
{
arraySum += [probabilitiesArray[i] intValue];
}
//Random a number from 0 to the sum of the array variables
int randomNumber = arc4random_uniform(arraySum);
//Iterating through the array variable and detect the right slot for the random number
int tempSum = 0;
for(int i = 0;i < probabilitiesArray.count;i++)
{
tempSum += [probabilitiesArray[i] intValue];
if(randomNumber < tempSum)
{
return i;
}
}
return 0;
}