Swift rand is not generating random numbers [duplicate] - swift

This question already has answers here:
Swift rand() not being random
(3 answers)
Closed 6 years ago.
I put rand() in Xcode Playground and it prints the same number 16807 even I run many times and value did not change -> "16807"!
is this a bug?
print(rand())

Mohamed Diaa,
You can make use of library functions like arc4random() or arc4random_uniform() for generating random numbers.
let random = Int(arc4random_uniform(3))
To get random value from 0 to 3
you might have to import Darwin to use it.

Related

Swift random element with probability [duplicate]

This question already has answers here:
Generate random numbers with a given distribution
(4 answers)
how to generate random numbers from 1 to 4 and 8 in swift
(2 answers)
Closed 4 months ago.
It seems like randomElement() in Swift only allows for an equally weighted selection. What would be the best (and most efficient way, as I need to repeat this very often) to select from a collection with pre-specified probabilities/weights?

Why is 500/1000 is printing 0? [duplicate]

This question already has answers here:
Math divison in Swift
(4 answers)
Is the Swift divide "/" operator not working or have I missed something?
(3 answers)
Closed 1 year ago.
I'm trying to understand why print(500/1000) print 0 ?
It should be 0.5 but it's not printing it?!
This happed to me when I started to show a progress bar based on total value and current value but I came across this issue and now I'm stuck
You are working with whole numbers, so it's returning a whole number.
If you use decimals, it should divide as expected.
I am not really in Swift, but I assume that in this language 500 and 1000 are both integer values (like in other programming languages).
Then 500/1000 is 0, with a remainder of 500.
If you want to have a fractional result, you should consider to use the proper datatype (I don't know what this is in Swift ... something like double or float, I assume): write 500.0/1000.0.

why ncread function in matlab reduce floating point numbers count? [duplicate]

This question already has answers here:
How to store more than 4 decimal places in an array in MATLAB
(2 answers)
Closed 5 years ago.
i have code below in matlab:
converted = ncread(this_file, 'U');
disp(converted(50,10,20));
and the result is:
-0.1561
actually the number is -0.15617890 but this code changes the the number of floating numbers. why?
MATLAB displays only 4 digits after the decimal point by default. You can use format to display more digits:
format long
converted = ncread(this_file, 'U');
disp(converted(50,10,20));

How to generate random numbers in Swift 2 Xcode 7? [duplicate]

This question already has an answer here:
arc4random_uniform not available in Xcode 7.0 beta (7a176x) on OSX 10.10.4
(1 answer)
Closed 7 years ago.
Have updated to Xcode 7, which has Swift 2. I am creating an app. I need to generate random numbers. In Swift, there used to be arc4random(). But in Swift 2, arc4random() is not there. Instead, I am getting:
arc4random_addrandom(<#T##UnsafeMutablePointer<UInt8>#>, <#T##Int32#>)
arc4random_stir()
arc4random_buf(<#T##UnsafeMutablePointer<Void>#>, <#T##Int#>)
How do I now generate random numbers? Any help would be appreciated a lot.
arc4random() and arc4random_uniform(_:) are actually there, they're just not autocompleting for some reason. You can still use them!

Swift semi random selection [duplicate]

This question already has answers here:
Generate random numbers with a given distribution
(4 answers)
Closed 7 years ago.
I want to "randomly" select from a collection in Swift but have certain instances be selected at different frequencies. Currently I am using.
let collection: [Int] = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
let random: Int = Int(arc4random_uniform(UInt32(collection.count)))
let selection: Int = collection[random]
I need the code to be compliant with Swift 2.0+.
If you're able to target iOS 9.0 or later, GameplayKit has a great solution called GKGaussianDistribution, which lets you generate random numbers in a range where the frequency of each number matches a bell curve like this:
To use this, just import GameplayKit into your project, then use this code:
let distribution = GKGaussianDistribution.d6()
print(distribution.nextInt())
That uses a six-sided die, but you can use any range you want. You might find my tutorial on GameplayKit random number generation useful.