隨機生成數字,字符的幾種方式(含數字轉字符串)

這些都是本人從網站上找來的,總結在一起,大家一起學習吧!

1、使用26個英文字母隨機產生10位的字符串

int?NUMBER_OF_CHARS =10;

char?data[NUMBER_OF_CHARS];

for?(intx=0;xchar)('A'?+(arc4random_uniform(26))));

NSString?*dataPoint =[[NSString alloc]initWithBytes:data?length:NUMBER_OF_CHARSencoding:NSUTF8StringEncoding];

2、產生隨機數字

(1)、?獲取一個隨機整數范圍在:[0,100)包括0,不包括100

int x =arc4random() % 100;

(2)、??獲取一個隨機數范圍在:[500,1000),包括500,不包括1000

int y =(arc4random() % 501) + 500;

(3)、??獲取一個隨機整數,范圍在[from,to),包括from,不包括to

-(int)getRandomNumber:(int)from to:(int)to

{

return (int)(from + (arc4random() % (to – from +1)));

}

(小數)doubleval = ((double)arc4random() /0x100000000);

數字轉換成字符串

objective c將整型轉換為字符型:

Convert Integer to NSString:

int Value = 112;

NSString *ValueString = [NSString stringWithFormat:@"%d", Value];

Convert NSString to C Integer:

int Value = 112;

int ValueString = [ValueintValue];

char *ValueasCString = (char *)[ValueString UTF8String];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容