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
}
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With