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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:22:21+00:00 2026-05-27T03:22:21+00:00

I’ve declared a static array of integer from 1 to 5 and now want

  • 0

I’ve declared a static array of integer from 1 to 5 and now want to call randomly each number with each press of a button. I don’t want the same number to be called twice.

int randomNumber;
static int size = 5;

int position = arc4random() % size - 1;
randomNumber = usedNumbers[position];
int switcher = usedNumbers[size]; // wrong
usedNumbers[position] = usedNumbers[size];
size--;
usedNumbers[position] = switcher;

Here’s what I’ve done so far. There’s a hole in my logic somewhere and I could do with some help. I think it’s something to do with the switcher which is trying to hold a number whilst another is being deleted.

  • 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-27T03:22:21+00:00Added an answer on May 27, 2026 at 3:22 am

    If all you want to do is to show a random number every time the button is clicked, here is some code that can help.

    But first, the line you use to create the position is wrong. Do this instead:

    int position = (arc4random() % 5) + 1; // Creates a random number between 1 and 5.
    int position = (arc4random() % 5) - 1; // Creates a random number between -1 and 3. 
    

    Second, I’d suggest to use an NSArray or NSMutableArray to hold your data.

    I assume that you have a method that is called when you press a button. Inside that method you can simply put something like this:

    int size = 5; // You might want to get the size of your array dynamically, with something like [usedNumbers count];
    int position = (arc4random() % size) + 1; // Generates a number between 1-5.
    NSNumber *randomNumber = [usedNumbers objectAtIndex:position]; // Here is your random number from the array.
    

    So.. If you add the array as an instance variable to your class, your header file would look something like this:

    @interface MyViewController : UIViewController
    
    @property (nonatomic, retain) NSMutableArray *usedNumbers;
    
    - (IBAction)buttonWasClicked:(id)sender; // Remember to connect it to your button in Interface Builder.
    
    @end
    

    And your implementation file:

    @implementation MyViewController
    
    @synthesize usedNumbers;
    
    - (void)viewDidLoad {
        // Initialize your array and add the numbers.
        usedNumbers = [[NSMutableArray alloc] init];
        [usedNumbers addObject:[NSNumber numberWithInt:4]];
        [usedNumbers addObject:[NSNumber numberWithInt:13]];
        // Add as many numbers as you'd like.
    }
    
    - (IBAction)buttonWasClicked:(id)sender {
        int size = [usedNumbers count];
        int position = (arc4random() % size); // Generates a number between 0 and 4, instead of 1-5.
        // This is because the indexes in the array starts at 0. So when you have 5 elements, the highest index is 4.
        NSNumber *randomNumber = [usedNumbers objectAtIndex:position]; // The number chosen by the random index (position).
        NSLog(@"Random position: %d", position);
        NSLog(@"Number at that position: %@", randomNumber);
    }
    
    @end
    

    If you do it like this, a random number will be chosen from the array every time the button is clicked.

    Also, remember to release all your objects if you don’t have ARC enabled.

    PS: There are several other questions here on SO covering this subject. Remember to search before asking.

    Update:

    To make sure every number is used only once, you can remove it from your array when it is chosen. So the buttonWasClicked: method will be something like this:

    - (IBAction)buttonWasClicked:(id)sender {
        int size = [usedNumbers count];
        if (size > 0) {
            int position = (arc4random() % size);
            NSNumber *randomNumber = [usedNumbers objectAtIndex:position];
            // Do something with your number.
            // Finally, remove it from the array:
            [usedNumbers removeObjectAtIndex:position];
        } else {
            // The array is empty.
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from

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.