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

  • Home
  • SEARCH
  • 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 5983117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:11:04+00:00 2026-05-22T22:11:04+00:00

Possible Duplicate: Generating Random Numbers in Objective-C Hi I am creating an app where

  • 0

Possible Duplicate:
Generating Random Numbers in Objective-C

Hi I am creating an app where when you press a pimple it travels to a random place in the view. THe pimple is a UIImageView. Here is the code that I put in:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *myTouch = [touches anyObject];

    CGPoint point = [myTouch locationInView:pimple];
    if ( CGRectContainsPoint(pimple.bounds, point) ) {
        [self checkcollision];
    }
}

-(void)checkcollision 
{

    pimple.center = CGPointMake(random(), random());    
    sad.hidden = NO;    
    NSURL* popURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"pop" ofType:@"mp3"]];
    AudioServicesCreateSystemSoundID((CFURLRef) popURL, &popID);
    AudioServicesPlaySystemSound(popID);
}

This code: pimple.center = CGPointMake(random(), random()); does not work. When I press the pimple, the pimple dissapears. Since I have not stated any .hidden for the pimple it must mean it IS moving. But you cant see it, (its outside the view.) Is there something wrong with my code? Am i missing something? Please help.

  • 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-22T22:11:05+00:00Added an answer on May 22, 2026 at 10:11 pm

    afaik random() ist just the same as rand() in c which

    Returns a pseudo-random integral number in the range 0 to RAND_MAX. […] RAND_MAX is a constant defined in cstdlib. Its default value may vary between implementations but it is granted to be at least 32767.

    As a result you move our pimple to a random position inside a rect that is much bigger than your window. Try this: (I assume that self is a UIView)

    pimple.center = CGPointMake(
        random() % (unsigned int)self.bounds.size.width, 
        random() % (unsigned int)self.bounds.size.height);
    

    The %-Operator is the Modulo-Operator.

    It seems that you dont seed your pseudo-random-generator which leeds to always the same trajectory on each startup. There is another Thread on stackoverflow which tells us to use arc4random instead of random()/rand()

    • 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 Objective-C How do I generate a random number
Possible Duplicate: generating random numbers from skewed normal distribution i expect a long array
Possible Duplicates: Generating Random Numbers in Objective-C What's the most optimal way to get
Possible Duplicate: Java: generating random number in a range I want to generate a
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: System.Random keeps on returning the same value I'm refactoring and expanding a
Possible Duplicate: True random number generator I have worked with random functions in python,ruby,
Possible Duplicates: Unique (non-repeating) random numbers in O(1)? How do you efficiently generate a
Possible Duplicate: Secure random number generation in PHP We need to generate a cryptographically
Possible Duplicate: python random string generation with upper case letters and digits How do

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.