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.

10
  • 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()?. Commented Jan 25, 2016 at 19:49
  • Possible duplicate of Generate random number in range with SecRandomCopyBytes Commented Jan 25, 2016 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 Commented Jan 25, 2016 at 20:04
  • No, its UIKit under single view application @LeoDabus Commented Jan 25, 2016 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 Commented Jan 25, 2016 at 20:29

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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?
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 (raywenderlich.com/category/swift).
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
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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.