I am having a strange problem in Swift 3.0 generating random numbers [in this case CGPoints]. I am trying to build a very simple particle filter and I am having trouble initially randomizing alll the particles using the code below.
When I iterate through my set of 'particles' a unique random point is generated [GOOD] but each particle gets the exact same unique random point [BAD]. I am not sure what I am doing wrong to ensure that each particle gets a unique random point
Anyone have any suggestions how I can force each particle to get a unique random point?
Thanks
Stan
Problem Function.
func randomize(
theListOfParticles: [RegionParticle]
) -> [RegionParticle] {
var returnListOfParticles = [SRLSystemRegionParticle]()
for particle in theListOfParticles {
particle.position = self.randomPointInRegion()
returnListOfParticles.append( particle )
}
return returnListOfParticles
}
Helper Function
/**
Get a random point in the region.
- Returns: CGPoint
*/
func randomPointInRegion() -> CGPoint {
let x : CGFloat = Math.randomCGFloat(
min: -(self.rect?.size.width)!,
max: (self.rect?.size.width)!
)
let y : CGFloat = Math.randomCGFloat(
min: -(self.rect?.size.width)!,
max: (self.rect?.size.width)!
)
let returnCGPoint : CGPoint = CGPoint(
x: x,
y: y
)
return returnCGPoint
}
The random gen function [in a separate class of all my math functions.]
/**
Generate a random CGFloat.
- Parameters:
- min: CGFloat
- max: CGFloat
- Returns: a CGFloat
*/
static func randomCGFloat( min: CGFloat, max: CGFloat) -> CGFloat {
return CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * (max - min) + min
}
Aucun commentaire:
Enregistrer un commentaire