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 6234089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:25:14+00:00 2026-05-24T10:25:14+00:00

Possible Duplicates: Generating random numbers in Objective-C iOS Random Numbers in a range Generate

  • 0

Possible Duplicates:
Generating random numbers in Objective-C
iOS Random Numbers in a range
Generate non-repeating, no sequential numbers

I am looking for method that give me random numbers between two numbers, and the first number will be number that I choose between the numbers.

for example, if I give this random function 5 and 10 and 9 as the first number, so it will give me: 9,10,7,6,5,8

I tried to use it:

NSUInteger count = [array count];
for (NSUInteger i = 1; i < count; ++i) {
        int nElements = count - i;
    int n = (random() % nElements) + i;
    while (n==`firstnumber`) {
        n = (random() % nElements) + i;
    }
}
  • 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-05-24T10:25:14+00:00Added an answer on May 24, 2026 at 10:25 am

    the first number is set by me and the other numbers by the method ,and
    every number is shown only one time

    It looks like you are after a shuffling algorithm. The following category on NSMutableArray will do the job:

    @interface NSMutableArray (Shuffling)
    - (void)shuffle;
    @end
    
    @implementation NSMutableArray (Shuffling)
    
    - (void)shuffle
    {
        // Fisher–Yates shuffle (modern algorithm)
        // To shuffle an array a of n elements (indexes 0..n-1):
        // for i from n − 1 downto 1 do
        //     j <-- random integer with 0 <= j <= i
        //     exchange a[j] and a[i]
        // http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
    
        for (int i = [self count] - 1; i >= 1; i--) {
            int j = arc4random() % (i + 1);
            [self exchangeObjectAtIndex:j withObjectAtIndex:i];
        }
    }
    
    @end
    

    Your requirement is that the number in the first position of the array is fixed (given by you). Then, you can do something like this:

    1. Populate the array with all numbers between minValue and maxValue (both included) except for firstValue.

    2. Shuffle the array.

    3. Insert firstValue at the first position in the array.

    Resulting in the following code:

    NSInteger minValue = 5;
    NSInteger maxValue = 10;
    NSInteger firstValue = 9;
    
    // minValue <= firstValue <= maxValue
    
    // populate the array with all numbers between minValue 
    // and maxValue (both included) except for firstValue
    NSMutableArray *ary = [NSMutableArray array];
    for (int i = minValue; i < firstValue; i++) {
        [ary addObject:[NSNumber numberWithInt:i]];
    }
    for (int i = firstValue + 1; i <= maxValue; i++) {
        [ary addObject:[NSNumber numberWithInt:i]];
    }
    // --> (5,6,7,8,10)
    // shuffle the array using the category method above
    [ary shuffle];
    // insert firstValue at the first position in the array
    [ary insertObject:[NSNumber numberWithInt:firstValue] atIndex:0];
    // --> (9,x,x,x,x,x)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Generating random numbers in Javascript Hi.. I want to generate random numbers
Possible Duplicate: Java: generating random number in a range How do I generate a
Possible Duplicate: Generating random results by weight in PHP? I have a web application
Possible Duplicate: Sha256 in Objective-C for iPhone Greetings, I'm having terrible trouble generating a
Possible Duplicates: Converting HTML Files to PDF PDF file generation from XML or HTML
Possible Duplicate: How an uninitialised variable gets a garbage value? How are garbage values
Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i ,
Possible Duplicate: Entity Framework Include() strongly typed Ok, I've got an entity framework query
Possible Duplicate: Is there a performance difference between i++ and ++i in C++? I
I consistently come across this code smell where I am duplicating markup, and I'm

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.