2012-03-13 46 views
44

我有兩個文本框,用戶可以輸入2個正整數(使用Objective-C)。目標是在這兩個數字之間返回一個隨機值。在Objective-C中的兩個數字之間生成隨機數字

我已經使用過「man arc4random」,但仍然無法將我的頭圍繞在它周圍。我已經想出了一些代碼,但它是越野車。

float lowerBound = lowerBoundNumber.text.floatValue; 
float upperBound = upperBoundNumber.text.floatValue; 
float rndValue; 
//if lower bound is lowerbound < higherbound else switch the two around before randomizing. 
if(lowerBound < upperBound) 
{ 
    rndValue = (((float)arc4random()/0x100000000)*((upperBound-lowerBound)+lowerBound)); 
} 
else 
{ 
    rndValue = (((float)arc4random()/0x100000000)*((lowerBound-upperBound)+upperBound)); 
} 

現在,如果我把值0和3,它似乎工作得很好。但是,如果我使用數字10和15,那麼對於「rndValue」,我仍然可以獲得低至1.0000000或2.000000的值。

我是否需要闡述我的算法,還是需要改變我使用arc4random的方式?

+0

http://iphonedevelopment.blogspot.com/2008/10/random-thoughts-rand-vs-arc4random.html – Andrew 2012-03-13 04:32:35

+1

你的括號是錯誤的。刪除'*'後面的左括號和';'前面的右括號。儘管你仍然有四捨五入的問題。 – 2012-03-13 04:34:53

+2

通用公式'minValue +((maxValue - minValue)* rnd())'其中'rnd()'返回0.0到1.0之間的隨機浮點值將返回您正在查找的值。我把它留給你轉換成Obj-C並優化它以適應你的需求。 – 2012-03-13 04:34:58

回答

116

你可以簡單地用整數值是這樣的:

int lowerBound = ... 
int upperBound = ... 
int rndValue = lowerBound + arc4random() % (upperBound - lowerBound); 

或者,如果你的意思是你想包括下界和上界之間的浮點數?如果是的話,請參考這個問題:https://stackoverflow.com/a/4579457/1265516

+1

int不是浮動 – Andrew 2012-03-13 04:37:24

+0

對不起,我誤解了你的問題,請參閱最新的答案。 – eveningsun 2012-03-13 04:40:09

+2

該代碼不包含upperBound作爲隨機輸出,請參閱下面的答案作爲修復。 – inexcii 2013-12-11 07:38:04

57

下面的代碼包括最小和最大值作爲隨機輸出數:

- (NSInteger)randomNumberBetween:(NSInteger)min maxNumber:(NSInteger)max 
{ 
    return min + arc4random_uniform((uint32_t)(max - min + 1)); 
} 

更新:
我用arc4random_uniform(upper_bound)更換arc4random() % upper_bound編輯答案正如@rmaddy所建議的那樣。
有關詳細信息,請參閱arc4random_uniform

更新2:
我更新了答案,通過在@bicycle指示的位置插入一個投影到uint32_t arc4random_uniform()

+6

使用'return arc4random_uniform(max - min + 1)+ min;'更好。 – rmaddy 2014-07-17 18:11:51

+0

@rmaddy你是對的!我已經更新了我的答案,並非常感謝您的建議。 – inexcii 2014-07-19 02:23:29

+0

@Demasterpl不需要浮點結果嗎? – 2014-07-19 02:53:03

2

如果可以的話,您應該避免使用mod(%)來限制值,因爲即使您使用的僞隨機數生成器(如arc4random)擅長於提供全範圍的均勻分佈的數字,在限制模數範圍內提供均勻分佈的數字。

你也不需要使用一個文字像0x100000000因爲在stdint.h提供一個方便的常數:

(float)arc4random()/UINT32_MAX 

這將使你在區間隨機浮動[0,1] 。請注意,arc4random在區間[0,2 ** 32 - 1]中返回一個整數。

要移動到這個你想要的時間間隔,你只需要添加您的最小值和你的範圍的大小乘以隨機浮動:

lowerBound + ((float)arc4random()/UINT32_MAX) * (upperBound - lowerBound); 

在你發佈你乘以隨機浮點代碼由整個混亂(lowerBound +(upperBound - lowerBound)),這實際上就等於upperBound。這就是爲什麼你仍然得到的結果低於你想要的下限。

7
-(int) generateRandomNumberWithlowerBound:(int)lowerBound 
           upperBound:(int)upperBound 
{ 
    int rndValue = lowerBound + arc4random() % (upperBound - lowerBound); 
    return rndValue; 
} 
0

Objective-C的功能

-(int)getRandomNumberBetween:(int)from to:(int)to 
{ 
    return (int)from + arc4random() % (to-from+1); 
} 

斯威夫特:

func getRandomNumberBetween(_ from: Int, to: Int) -> Int 
{ 
    return Int(from) + arc4random() % (to - from + 1) 
} 

通過調用它的任何地方:

int OTP = [self getRandomNumberBetween:10 to:99]; 
NSLog(@"OTP IS %ld",(long)OTP); 
NSLog(@"OTP IS %@",[NSString stringWithFormat @"%ld",(long)OTP]); 

對於斯威夫特:

var OTP: Int = getRandomNumberBetween(10, to: 99) 
0

在斯威夫特:

let otp = Int(arc4random_uniform(6)) 

試試這個。