在iPhone应用中如何使用随机数(How to Use Random Numbers in Your iPhone App)
来源:互联网 发布:免费网络代理服务器 编辑:程序博客网 时间:2024/05/17 07:48
原文地址:http://howtomakeiphoneapps.com/2009/05/how-to-use-random-numbers-in-your-iphone-app/
Would you like your iPhone app to be able randomly pick a number between 1 and 10 or to randomly select one string from a list?
Getting this done requires us to use the random function - a regular old C function. In this post, I am going to show you how to create an array of objects and then use a random number to select one object from the list.
FIrst, use the import statements to import these two libraries: stdlib and time. Put this code into the top of your file:
#import "stdlib.h"
#import "time.h"
Now, create an array and populate it with strings that will serve as our objects:
NSMutableArray *arrayOfObjects = [[NSMutableArray alloc] init];
[arrayOfObjects addObject:@"Object One"];
[arrayOfObjects addObject:@"Object Two"];
[arrayOfObjects addObject:@"Object Three"];
[arrayOfObjects addObject:@"Object Four"];
[arrayOfObjects addObject:@"Object Five"];
Set the seed to the system clock. This will ensure that you get different results every time your run your app.
srandom(time(NULL));
Here is how to get a random number - the number at the end of the statement indicates what the upper limit is. However, the function returns numbers starting with 0 so in practice you will get five results ranging from 0 to 4.
int r = random() % 5;
At this point we have the random number assigned to r and we can use it in any way we like. This is an example of using this number to pick an object our of our array.
NSLog(@"Object = %@", [arrayOfObjects objectAtIndex:r]);
As you can imagine, there are a lot of fun things that you could do with the random function: game logic, random thing of the week and so on.
What would you like to use the random function for in your app?
- 在iPhone应用中如何使用随机数(How to Use Random Numbers in Your iPhone App)
- 如何在Android程序中使用ACRA3(How to install ACRA3 in your Android application)
- 如何在Android程序中使用ACRA3(How to install ACRA3 in your Android application)
- iOS Programming 101: How To Send Email in Your iPhone App
- How to use user’s location in your app?
- How to use Display Zoom on your iPhone 6 or iPhone 6 Plus
- [HOW TO]如何在iPhone应用程序中发送邮件
- 如何在C++中使用VBScript(使用Qt)--How to use VBScript in C++ (Using Qt)
- How to use ComboBox Control in VC++ - 在VC中如何使用 ComboBox 控件
- 如何在python中使用*args,**kwargs (How to use *args and **kwargs in Python)
- How to use bitBucket with EGit in Eclipse,如何在Eclipse中使用bitbucket远程仓库
- How to Add iAd Banner in iPhone App
- [iphone开发]UISlider tutorial - example how to use slider in iPhone SDK / XCode
- 使用 Shark 来对你的 iPhone 应用进行性能调优(Using Shark to Performance Tune Your iPhone App)
- (zz)How to sideload apps on your iPhone using Xcode 7(无需99刀也能在真机上测试App)
- How to SSH Into Your iPhone
- How to use Log4net in your project
- How to use logrotate to manage log files in linux(如何在Linux中使用logrotate去管理日志文件)
- JavaFX+Java打字练习软件
- Objective-C中的For和ForEach循环(For and ForEach Loops in Objective-C )
- java下Class.forName的作用是什么,为什么要使用它
- w to Use a Dictionary in Objective-C
- linux设备驱动的周期事件
- 在iPhone应用中如何使用随机数(How to Use Random Numbers in Your iPhone App)
- 错误检测和纠错简述
- JTA
- 一个在加拿大上学的朋友的暑假作业
- 行为模式之九---Strategy
- OSPF原理知识剖析
- 给iPhone应用开发的有志者的100个免费的课程和学习指南(100 Free Courses & Tutorials for Aspiring iPhone App Developers)
- 《JAVA编程思想》第四版学习 需要我记住的something--多态
- 面试