转载自:http://hi.baidu.com/bunsman/blog/item/9c7cf79ba2261cacc9eaf4e4.html
参考:http://www.friendlydeveloper.com/2010/02/objective-c-get-random-number/comment-page-1/
-(int)getRandomNumber:(int)from
to:(int)to {
return(int)(from
+ arc4random() % (to-from+1));
}
How to use:
Implement method above into your .m file
Add the following line to your .hfile:
1)
-(int)getRandomNumber:(int)from
to:(int)to;
2) Call the method like:
intrandomNumber = [selfgetRandomNumber:9to:99];
//this gets you a random number between 9 and 99
关于生成多个不重复随机数的问题。
我的解决思路是,先创建一个MutableArray,放入随机数的取值范围,然后从0到[mutableArray count]生成的随机数index,去取mutable的值。取一个然后再remove掉。就可以得到不重复的随机数了。
示例代码:
//生成多个随机数
NSNumber*irandom1, *irandom2, *irandom3, *irandom4;
NSUIntegercapacity = [dataArraycount]-1;//dataArray是取值的源数组
NSMutableArray*container = [[NSMutableArrayalloc]initWithCapacity:capacity];
for(NSUIntegeri =0;
i < [dataArraycount] ; i++) {
NSNumber*nsi = [NSNumbernumberWithInt:i];
[containeraddObject:nsi];
}
NSUIntegerindex = [setintValue];
[containerremoveObjectAtIndex:index];
index = (arc4random() % [containercount]);
irandom1 = [containerobjectAtIndex:index];
[containerremoveObjectAtIndex:index];
index = (arc4random() % [containercount]);
irandom2 = [containerobjectAtIndex:index];
[containerremoveObjectAtIndex:index];
index = (arc4random() % [containercount]);
irandom3 = [containerobjectAtIndex:index];
[containerremoveObjectAtIndex:index];
index = (arc4random() % [containercount]);
irandom4 = [containerobjectAtIndex:index];
[containerrelease];
分享到:
相关推荐
学习Objective-C: 入门教程
Cocoa and Objective-C: Up and Running Foundations of Mac, iPhone, and iPod touch programming By: Scott Stevenson Publisher:O'Reilly Media Released: March 2010 Pages: 416 Build solid applications for ...
Objective-C Programming: The Big Nerd Ranch Guide (2nd Edition).epub (epub 格式) Want to write iOS apps or desktop Mac applications? This introduction to programming and the Objective-C language is ...
这儿的运行时系统扮演的角色类似于Objective-C语言的操作系统,Objective-C基于该系统来工作。本文档将具体介绍NSObject类以及Objective-C程序是如何和运行时系统交互的。特别地,本文档还给出来怎样在运行时动态地加
Objective-C: From Zero to Hero - Object Oriented Programming 1
Objective-C: From Zero to Hero - Object Oriented Programming 2
Objective-C的语法与Cocoa框架.pdf,挺不错的教程
Objective-C语言的许多决策可以在编译和运行时执行。只要有可能,它是动态的。这意味着Objective-C语言不仅需要一个编译器,还需要一个运行时系统来执行编译的代码。Runtime系统是一种用于Objective-C语言的操作系统...
Object-C语言教程:Objective-C 简介
《objective-c程序设计》通过大量的实例系统地介绍了objective-c语言的基本概念、语法规则、框架、类库及开发环境。读者在阅读《objective-c程序设计》后,可以掌握objective-c语言的基本内容,并进行实际的iphone/...
希望这个简单的Objective-C语言教程能够为你提供一个入门的起点。Objective-C是一种面向对象的编程语言,广泛应用于Mac OS和iOS开发。通过学习Objective-C的基本语法、类和对象、控制流程和方法等内容,你将能够编写...
用Objective-C语言实现了各种设计模式,收集各种例子,方便大家学习和普及设计模式。.zip用Objective-C语言实现了各种设计模式,收集各种例子,方便大家学习和普及设计模式。.zip用Objective-C语言实现了各种设计...
《Objective-C 程序设计(第4版)》作者假设读者没有面向对象程序语言或者C语言(Objective-C基础)编程经验,因此,初学者和有经验的程序员都可以使用这本《Objective-C 程序设计(第4版)》学习Objective-C。...
The book makes no assumptions about prior experience with object-oriented programming languages or with the C language (which Objective-C is based upon). Because of this, both beginners and ...
Objective-C基础教程:1天玩转Objective-C语法
C语言主要知识点巩固(学习Objective-C 的前提),PPT格式。
Objective-C高级编程 iOS与OS X多线程和内存管理.pdf
中文名: Objective-C基础教程 作者: Mark Dalrymple Scott Knaster译者: 高朝勤 杨越 刘霞图书 分类: 软件 资源格式: PDF 版本: 扫描版 出版社: 人民邮电出版社 书号: ISBN: 9787115208774 发行时间: 2009...
elcome to Learn Objective- C on the Mac! This book is designed to teach you the basics of the Objective- C language. Objective- C is a superset of C and is the language used by many (if not most) ...
在windows系统下搭建Objective-C开发环境,由于公司要做iphone开发,个人学习objc(Objective-C)语法,所以在windows7下搭建了一个objc开发环境,现把自己整理的帮助文档拿来与大家分享,希望能对大家有所帮助。...