-2

I would like the images to appear more randomly than they do with this code:

//placing images on the screen
-(void)PlaceImage {

    RandomImagePosition = arc4random() %1000;
    Image.center = CGPointMake(570,  RandomImagePosition);

    // the higher the number (570) the farther to the right the platforms appear
}

They appear in different positions but most of the time towards to top of the screen. There will be a few times when the image is placed towards the bottom of the screen. I would like there to be more randomness.

dev_help
  • 119
  • 10
  • 1
    What's the question? – Ben Zotto Aug 15 '14 at 17:23
  • Hello, welcome to the site! This is a bad question. I recommend you read [the entire Help Center](http://stackoverflow.com/help) and head back when you have a specific, programming-related question. – admdrew Aug 15 '14 at 17:23
  • 1
    often when things "don't seem as random" as you want, it's because you don't really want random; you want to avoid the concentrations that happen in random data. You probably need to understand randomness better and better articulate what you want. Also using `%` to produce distributions is a bad idea. Take a look at the c++ `` library and the distributions it offers. – bames53 Aug 15 '14 at 17:25

2 Answers2

3

Use arc4random_uniform to generate a random integer in a specified range. Never use arc4random mod something; that is indeed biased and will produce suboptimal results.

If you have further issues with "randomness" the you should look carefully at how you are using your random value. Notably, people's perceptions of "random" are often quite different from mathematical random: for instance, people expect "random" coin flips to switch between heads and tails much more frequently than actual random will produce. Therefore, to make something perceptually random, you may have to fudge the output a bit (e.g. to reduce the chance that a value will repeat twice).

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • How so? You just call it as arc4random_uniform(700) and don't use modulo with it. It should produce the same output but less biased. – nneonneo Aug 15 '14 at 17:32
  • 1
    Never use the term "breaking" without context. There isn't any way I can read your mind and tell how it is broken. – nneonneo Aug 15 '14 at 17:33
  • every time an image leaves the screen and a new image is called to be placed on the screen the simulator pauses and says there is a breakpoint right at arc4randim_uniform(700) – dev_help Aug 15 '14 at 17:35
  • 2
    Just [remove the breakpoint](http://stackoverflow.com/questions/10016890/thread-1-stopped-at-breakpoint-error-when-initializing-an-nsurl-object/10016939#10016939), @JamesF. – jscs Aug 15 '14 at 17:36
1

You are likely experiencing modulo bias and should be using arc4_random_uniform(700). From man arc4random:

 arc4random_uniform() will return a uniformly distributed random number
 less than upper_bound.  arc4random_uniform() is recommended over con-
 structions like ``arc4random() % upper_bound'' as it avoids "modulo bias"
 when the upper bound is not a power of two.
Brian Nickel
  • 26,890
  • 5
  • 80
  • 110