简体   繁体   中英

How to generate random 6 digits number by a button?

I tried random generate number from 1 to 100.How to change random 6 digits ?

Note : Numbers can not start with 0(zero)

Random from 1 to 100 codes

 #import <UIKit/UIKit.h>

 @interface RandomNumberGenViewController : UIViewController {

 int number;
 IBOutlet UILabel *label;

 }

 -(IBAction)generateNumber:(id)sender;

 @end



 @implementation RandomNumberGenViewController

 -(IBAction)generateNumber:(id)sender {

 number = (arc4random()%100)+1; //Generates Number from 1 to 100.
 NSString *string = [NSString stringWithFormat:@"%i", number];
 label.text = string

 }

在此输入图像描述

A random number with 6 digits would be:

int number = arc4random_uniform(1000000);

But that gives a number from 0 to 999,999. It sounds like you want a random number from 100,000 to 999,999. So do this:

int number = arc4random_uniform(900000) + 100000;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM