Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9144505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:17:10+00:00 2026-06-17T10:17:10+00:00

I have a random number generator method in iOS that I am currently using.

  • 0

I have a random number generator method in iOS that I am currently using. While it is working fine, I would like to modify it such that in its output, it ALWAYS includes at least one number. How would I do this? Here is the random number method that I am using:

- (NSString *)generateRandString {

    NSString *alphabet  = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789";
    NSMutableString *s = [NSMutableString stringWithCapacity:5];
    for (NSUInteger i = 0U; i < 5; i++) {

        u_int32_t r = arc4random() % [alphabet length];
        unichar c = [alphabet characterAtIndex:r];
        [s appendFormat:@"%C", c];

    }

    return s;
}

Thanks in advance to all who reply.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:17:11+00:00Added an answer on June 17, 2026 at 10:17 am

    This is my edited and checked solution:

    - (NSString *)generateRandString {
    
             NSString *alphabet  = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789";
             NSMutableString *s;
             NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    
             do
            {
                s = [NSMutableString stringWithCapacity:5];
               for (NSUInteger i = 0U; i < 5; i++)
               {
    
                  u_int32_t r = arc4random() % [alphabet length];
                  unichar c = [alphabet characterAtIndex:r];
                  [s appendFormat:@"%C", c];
                }
           }
           while ([s rangeOfCharacterFromSet:set].location == NSNotFound);  
    
          return s;
     }
    

    I’ve also implemented the solution suggested by Dan Dyer (and it’s obviously better than mine). My implementation is probably not perfect ( i use the same functionality few times instead of creating a separate function for it, but i believe it’s not difficult to implement for you) but it works exactly as Dan has proposed.

      NSString *alphabet  = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789";
    NSMutableString *s  = [NSMutableString string];
    NSCharacterSet *set = [NSCharacterSet decimalDigitCharacterSet];
    NSString *digits    = @"0123456789";
    
    for (NSUInteger i = 0U; i < 4; i++)
    {
        u_int32_t r = arc4random() % [alphabet length];
        unichar c = [alphabet characterAtIndex:r];
        [s appendFormat:@"%C", c];
    }
    
    if ([s rangeOfCharacterFromSet:set].location == NSNotFound)
    {
        u_int32_t r = arc4random() % [digits length];
        unichar c = [digits characterAtIndex:r];
        [s appendFormat:@"%C", c];
    }
    else
    {
        u_int32_t r = arc4random() % [alphabet length];
        unichar c = [alphabet characterAtIndex:r];
        [s insertString:[NSString stringWithFormat:@"%C", c] atIndex:arc4random() % s.length];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the Xorshift random number generator... I already have the generator, but I haven't
I would like to have a random number generated based of a variable called
I have a function that generates normal random number matrix having normal distribution using
Possible Duplicate: Random number generator only generating one random number I have a method
Let's say you have a random number generator spitting out numbers between 1 and
Possible Duplicate: True random number generator I have worked with random functions in python,ruby,
Like my question, i need to generate random numbers that have identical pairs between
Possible Duplicate: Random number generator only generating one random number Random not that random
I am using AndEngine. I have a Method that adds targets to the screne.
I'm currently working on a library to generate synthetic fingerprints using the SFinGe method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.