回答
char c = (char)('A' + arc4random_uniform(25))
會給你一個隨機大寫字符。
不應該是** arc4random_uniform(26)**?由於arc4random_uniform(25)的最大值是24(根據[手冊頁](https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man3/arc4random_uniform.3.html) ),'A'+ 24是'Y'。 – Gyfis
將所有大寫字母存儲在數組中,並使用rand()或arcg4rand()從0-25生成隨機數,然後分別檢索對象和隨機編號索引。
爲什麼你會打擾所有的數組操作,當你可以做'A'+ your_random_number ... – verdesmarald
是啊..你說得對 – Allamaprabhu
NSMutableArray *a = [[NSMutableArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", nil]; // You can add more uppercases here.
int firstRandom = objectAtIndex:arc4random()%[a count];
NSString *s = [[NSString alloc] initWithFormat:@"%@", [a objectAtIndex:firstRandom]];
[a removeObjectAtIndex:firstRandom];
// Avoiding some uppercase repetitions. ;-)
for (int i = 0; i < [a count]; i++) {
int rndm = objectAtIndex:arc4random()%[a count];
s += [a objectAtIndex:rndm];
[a removeObjectAtIndex:rndm];
}
NSLog(@"%@", s);
[a release];
[s release];
希望回答這是你想要的。 :-)
阿爾貝託
- 1. 在c中生成隨機字符
- 2. 如何生成大的隨機數C
- 3. 通過隨機字符生成生成隨機名字太慢
- 4. 在C中生成隨機數字
- 5. 生成隨機字符串
- 6. 生成隨機ASCII字符
- 7. Android隨機生成字符
- 8. 在iPhone中使用NSArray生成隨機字符串?
- 9. 如何在C++中生成非常大的隨機數
- 10. 從C#中的字符串列表生成隨機字符串?
- 11. 如何生成隨機模塊不區分大小寫的字符串
- 12. 如何獲得目標中的隨機Unicode字符C
- 13. 在目標c中生成唯一的隨機數?
- 14. 在iPhone中生成隨機值
- 15. 如何在Dart中生成大的隨機數字?
- 16. 如何在Python中生成一個「大」的隨機數字?
- 17. 如何在Haskell中隨機生成一個隨機生成的數字列表
- 18. 如何在Ruby中隨機化字符串的大小寫?
- 19. 如何用C#中的權重編寫隨機數生成器?
- 20. 在C++中生成隨機字符和整數
- 21. 如何在C#中生成隨機數字?
- 22. 如何在C++中生成隨機二進制數字?
- 23. 如何在c中生成唯一的隨機數字#
- 24. 如何在Rational Performance Tester中生成一個隨機字符串?
- 25. 如何在java中生成隨機字符串?
- 26. 如何在Python中同步隨機生成的字符串
- 27. 如何在TSQL中生成隨機格式化的字符串
- 28. 生成奇怪字符的隨機字符串生成
- 29. 由openssl生成的隨機字符串並非如此隨機。
- 30. PHP隨機字符串生成器...並非如此隨機
如何使用ascii http://stackoverflow.com/a/2832750/926460 – Timeless