Swift - Seeding Arc4Random_Uniform? or Alternative

Swift - Seeding arc4random_uniform? Or alternative?

I know "GameKit" sounds like it's just for games, but it contains a serious random number generation system. I suggest you take a look at GKMersenneTwisterRandomSource and GKRandomDistribution. The GKMersenneTwisterRandomSource takes a random seed (if you so choose) and the GKRandomDistribution class implements a Uniform Distribution. Used together, they do exactly what you're looking for.

import GameKit

// The Mersenne Twister is a very good algorithm for generating random
// numbers, plus you can give it a seed...
let rs = GKMersenneTwisterRandomSource()
rs.seed = 1780680306855649768

// Use the random source and a lowest and highest value to create a
// GKRandomDistribution object that will provide the random numbers.
let rd = GKRandomDistribution(randomSource: rs, lowestValue: 0, highestValue: 100)

// Now generate 10 numbers in the range 0...100:
for _ in 1...10 {
print(rd.nextInt())
}

print("---")

// Let's set the seed back to the starting value, and print the same 10
// random numbers.
rs.seed = 1780680306855649768
for _ in 1...10 {
print(rd.nextInt())
}

seeding arc4random() in iOS

That's not what arc4random is designed to do. As the documentation states:

The arc4random() function provides a high quality 32-bit pseudo-random
number very quickly. arc4random() seeds itself on a regular basis from
the kernel strong random number subsystem described in random(4).

Since it is re-seeds itself from an entropy source anyway, you gain nothing by seeding it manually, and in fact, such a method does not exist.

What is the equivalent of seeded random in Swift3 (Xcode8 beta 1)

You can use
srand48(seed) and drand48() in Swift3.

Using arc4random_uniform to return a both whole and non whole doubles

To generate a Double in the range 0.0 to 300.0 (with one digit following the decimal):

Double(arc4random_uniform(3001))/10.0

You can extend this to more decimal places. For two decimal places (0.00 to 300.00):

Double(arc4random_uniform(30001))/100.0

For three decimal places (0.000 to 300.000):

Double(arc4random_uniform(300001))/1000.0

This has the advantage of being able to actually generate whole values. In the first case 10% of the numbers will be whole. In the second case 1% of the numbers will be whole. And in the third, 0.1% of the numbers will be whole.

Can someone help me stop an arc4random generated number from appearing again?

try this:

var randomAssets: Set<Int> = Set(0...18)

@IBAction func Button(_ sender: Any) {
guard let random = randomAssets.randomElement() else {
return
}
randomAssets.remove(random)
Smallbug.image = UIImage(named: "Bug\(random)")
}

But there is a warning, after 18 times it stop working obviously.

Generate random values based on a seed Swift 3

If you want to generate a random value based on a specific seed use: srand48 and drand48:

srand allow you to specify the seed:

    srand48(230)

Then drand48 will give you a double between 0 and 1:

    let doubleValue = drand48()

So in your case, if you want an Int between 0 and 9, you can do something like:

    srand48(230)
print("Random number: \(1 + Int((drand48() * 8) + 0.5))")
print("Random number: \(1 + Int((drand48() * 8) + 0.5))")
print("Random number: \(1 + Int((drand48() * 8) + 0.5))")

It will give you the 3 pseudo random numbers until you change the seed

Here is the drand48 applied to your new question:

let seed = srand48(230)

for column in 0 ..< tileMapNode.numberOfColumns {
for row in 0 ..< tileMapNode.numberOfRows {
let rand = Int(Double(tileSet.tileGroups.count) * drand48() - 0.5)
print(rand)
let tile = tileMapNode.tileSet.tileGroups[rand]
tileMapNode.setTileGroup(tile, forColumn: column, row: row)
}
}


Related Topics



Leave a reply



Submit