Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

 }

enter image description here

like image 717
Mhmt Avatar asked Jul 17 '13 19:07

Mhmt


People also ask

How do you generate a 6 digit random number in DART?

you can get a random number in this range (900000) and add 100000 to the random number you get: var rng = new Random(); var code = rng. nextInt(900000) + 100000; This will always give you a random number with 6 digits.

How do I generate a random 6 digit number in node JS?

random() generates a random number between 0 and 1 which we convert to a string and using . toString() and take a 6 digit sample from said string using . substr() with the parameters 2, 6 to start the sample from the 2nd char and continue it for 6 characters. This can be used for any length number.


1 Answers

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;
like image 153
rmaddy Avatar answered Sep 25 '22 19:09

rmaddy