简体   繁体   中英

Generate a Random Number between 0.0001 and 0.002 in Objective C (iOS)?

Does anyone know how I could generate a random number in a range in iOS? I am currently working on a synthesizer in iOS (using SpriteKit and AudioKit) and I am trying to modify the loudness of the synth with changing its variability whenever a slider is being moved as well.

This is what my code looks like:

[Synth setAmplitude: 0.5 + (slider.currentValue * loudnessVar)];

where 0.5 is the default amplitude value and loudnessVar is a random number.

Since, the slider returns values from -170 to 170 , I would need a relatively low number in order to set a value between 0 and 1.

Is anyone able to help with this?

The way to generate a random number in a range is:

NSInteger random = min + arc4random() % (max - min);

So, you can generate a number between 1-20 and divide it by 1000, it's just an example.

I.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM