1

I'm fairly new to Swift, only having used Python and Pascal before. I was wondering if anyone could help with generating a floating point number in range. I know that cannot be done straight up. So this is what I've created. However, it doesn't seem to work.

func location() {
    // let DivisionConstant = UInt32(1000)

    let randomIntHeight = arc4random_uniform(1000000) + 12340000
    let randomIntWidth = arc4random_uniform(1000000) + 7500000

    XRandomFloat = Float(randomIntHeight / UInt32(10000))
    YRandomFloat = Float(randomIntWidth / UInt32(10000))

    randomXFloat = CGFloat(XRandomFloat)
    randomYFloat = CGFloat(YRandomFloat)

    self.Item.center = CGPointMake(randomXFloat, randomYFloat)
}

By the looks of it, when I run it, it is not dividing by the value of the DivisionConstant, so I commented this and replaced it with a raw value. However, self.Item still appears off screen. Any advice would be greatly appreciated.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jonnyr612
  • 187
  • 1
  • 1
  • 8
  • 1
    "It doesn't seem to work" and "by the looks of it ..." are pretty vague problem descriptions. What values do you get and what do you expect? – The question itself is (for example) answered here: [How does one make random number between range for arc4random_uniform()?](http://stackoverflow.com/questions/24132399/how-does-one-make-random-number-between-range-for-arc4random-uniform). – Martin R Jan 25 '16 at 19:49
  • Possible duplicate of [Generate random number in range with SecRandomCopyBytes](http://stackoverflow.com/questions/30783640/generate-random-number-in-range-with-secrandomcopybytes) – Alexander Perechnev Jan 25 '16 at 19:55
  • Im sorry, Thank you for your fast reply, What i mean is that i am doing it as an iPhone screen view, and i want it to not touch the edge, so be in 100px from the edge, unfortunately, i have tried my current code, and it seems to generate off of the visible screen, which it shouldn't given the dimensions Etc. I would have expected it to show on screen, just shy of touching the edge, however It does not do as expected. I had a look at the attached link, however my issue was more with why it wasn't showing on screen. Im sorry for the ambiguity, Thanks, @MartinR – Jonnyr612 Jan 25 '16 at 20:04
  • No, its UIKit under single view application @LeoDabus – Jonnyr612 Jan 25 '16 at 20:05
  • Im trying to create a CGPoint, so i require an X coordinate, and a Y coordinate, and they have to start from 100 - 1234 and 100 - 750 @LeoDabus – Jonnyr612 Jan 25 '16 at 20:29
  • Just add 100 to the random result of 1135 – Leo Dabus Jan 25 '16 at 20:30
  • `CGFloat(arc4random_uniform(1135) + 100)` – Leo Dabus Jan 25 '16 at 20:33
  • @LeoDabus Thank you for this, this does help my situation, however, i cannot work out why it appears off screen, as i am trying to make it so it doesn't leave the screen at all, Thanks, Jonny – Jonnyr612 Jan 25 '16 at 20:47
  • What's the size of your element? And what is it? – Leo Dabus Jan 25 '16 at 20:50
  • By element I assume you mean the item i am trying to display at a random location - It is about 50 px from centre to edge, however i require double that to scale, and its an object for a simple game, I'm trying to show it onscreen, without it touching the edge of the screen of an iPhone 6 in portrait orientation. @LeoDabus – Jonnyr612 Jan 25 '16 at 20:52

2 Answers2

1

This division probably isn't what you intended:

XRandomFloat = Float(randomIntHeight / UInt32(10000))

This performs integer division (discarding any remainder) and then converts the result to Float. What you probably meant was:

XRandomFloat = Float(randomIntHeight) / Float(10000)

This is a floating point number with a granularity of approximately 1/10000.

Your initial code:

let randomIntHeight = arc4random_uniform(1000000) + 12340000

generates a random number between 12340000 and (12340000+1000000-1). Given your final scaling, that means a range of 1234 and 1333. This seems odd for your final goals. I assume you really meant just arc4random_uniform(12340000), but I may misunderstand your goal.


Given your comments, I think you've over-complicated this. The following should give you a random point on the screen, assuming you want an integral (i.e. non-fractional) point, which is almost always what you'd want:

let bounds = UIScreen.mainScreen().bounds

let x = arc4random_uniform(UInt32(bounds.width))
let y = arc4random_uniform(UInt32(bounds.height))
let randomPoint = CGPoint(x: CGFloat(x), y: CGFloat(y))

Your problem is that you're adding the the maximum value to your random value, so of course it's always going to be offscreen.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Im Trying to make it so that i can display and image on screen. To make a CGPoint, i require a CGFloat for both the X axis and the Y axis, Im trying to generate random for both, so that it shows up in a random location on an iPhone Screen, however, it appears off of the screen, for reasons that i am unaware, Im looking for help to A. make the CGFloats for the CGPointMake, and B. to work out why it is showing off screen. I thought that By doing the Division, i could make the CGFloats, but I'm not fully aware, would you be able to advise please? – Jonnyr612 Jan 25 '16 at 20:44
  • Thank you so much, would that mean that there is a gap of at least k between the object and the edge of the screen though? – Jonnyr612 Jan 25 '16 at 22:59
  • What is "k"? If you want to adjust the number to fit within some range, add the minimum you want, and reduce the maximum to fit inside the range. Look up the documentation on arc4random_uniform. It should be very obvious what this code is doing. If it isn't, you need to go back to basics (http://www.raywenderlich.com/category/swift). – Rob Napier Jan 25 '16 at 23:15
  • Oh K was just a random letter i chose to represent the distance from the edge, in maths K is used to represent a constant, I'm sorry this wasn't clear – Jonnyr612 Jan 26 '16 at 08:59
0

I'm not sure what numbers you're hoping to generate, but what you're getting are results like:

1317.0, 764.0
1237.0, 795.0
1320.0, 814.0
1275.0, 794.0
1314.0, 758.0
1300.0, 758.0
1260.0, 809.0
1279.0, 768.0
1315.0, 838.0
1284.0, 763.0
1273.0, 828.0
1263.0, 770.0
1252.0, 776.0
1255.0, 848.0
1277.0, 847.0
1236.0, 847.0
1320.0, 772.0
1268.0, 759.0

You're then using this as the center of a UI element. Unless it's very large, it's likely to be off-screen.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • Thank you, I was und3er the impression that the edges of the iPhone 6 screen was 1334 x 750, and to be offset by 100 so as to not quite touch the edge, would you be able to suggest which numbers will be reasonable to use? I thought that it would generate maybe 9368231, 4139023 and then to be divided by 10000 to make the coordinate 936.8231, 412.9023 to give me a CGFloat, which i can then use for CGPointMake. Thanks, Jonny – Jonnyr612 Jan 25 '16 at 20:33