在iPhone应用中如何使用随机数(How to Use Random Numbers in Your iPhone App)
2009-06-10 22:16
951 查看
原文地址: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:
Now, create an array and populate it with strings that will serve as our objects:
Set the seed to the system clock. This will ensure that you get different results every time your run your app.
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.
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.
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?
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?
相关文章推荐
- 如何在Vista中使用MMC方式的Remote Desktops(How to use Remote Desktops through mmc console in Vista)
- All-In-One Code Framework(AIO): 如何编写VC++代码来使用COM组件 (How to use COM in a VC++ client)
- HowTo use linux laptop in presentations 【如何在Linux系统上使用投影仪】
- 如何在python中使用*args,**kwargs (How to use *args and **kwargs in Python)
- [Ubuntu] 如何在VirtualBox OSE中使用share folders -- How to use the share folders in VirtualBox OSE
- iOS Programming 101: How To Send Email in Your iPhone App
- 如何在C++中使用VBScript(使用Qt)--How to use VBScript in C++ (Using Qt)
- How to use ComboBox Control in VC++ - 在VC中如何使用 ComboBox 控件
- iphone dev 入门实例7:How to Add Splash Screen in Your iOS App
- 如何使用th:each属性迭代模板-原标题:How To Use th:each For Iteration In Thymeleaf Template?
- How to use user’s location in your app?
- How to use bitBucket with EGit in Eclipse,如何在Eclipse中使用bitbucket远程仓库
- How to use user’s location in your app?
- How to use toolbar control on dialog using CToolBar class in VC++ - VC++ 中用CToolBar 类如何使用ToolBar控件
- [转]How to use String.Format in PowerShell?(如何在PowerShell中使用string.format?)
- Instruments on Leopard: How to debug those random crashes in your Cocoa app
- 如何在QT for S60 中使用pthread库(Carbide.c++) (How to use pthread in QT4.6 for S60)
- How to use logrotate to manage log files in linux(如何在Linux中使用logrotate去管理日志文件)
- 使用 Shark 来对你的 iPhone 应用进行性能调优(Using Shark to Performance Tune Your iPhone App)
- Android:How to use loader in Android(如何使用Loader)