Random Swift3(Xcode8 beta 1)中的种子随机的等价物是什么
我需要在每次执行我的应用程序时启动相同的随机数列表。 srand/rand不再存在。那我该怎么办Random Swift3(Xcode8 beta 1)中的种子随机的等价物是什么,random,srand,swift3,Random,Srand,Swift3,我需要在每次执行我的应用程序时启动相同的随机数列表。 srand/rand不再存在。那我该怎么办 private extension Array { private func randomValues(_ seed: UInt32, num: Int) -> [Element] { srand (seed) var indices = [Int]() indices.reserveCapacity(num) let
private extension Array {
private func randomValues(_ seed: UInt32, num: Int) -> [Element] {
srand (seed)
var indices = [Int]()
indices.reserveCapacity(num)
let range = 0..<self.count
for _ in 0..<num {
var random = 0
repeat {
random = randomNumberInRange(range)
} while indices.contains(random)
indices.append(random)
}
return indices.map { self[$0] }
}
专用扩展数组{
私有func随机值(useed:UInt32,num:Int)->[元素]{
srand(种子)
var指数=[Int]()
指数.储备容量(num)
让range=0..我找不到在Swift 3 Beta 1中使用seed random的方法。必须用C编写一个愚蠢的包装函数:
// ----------------------------------------------
// my_random.h
// ----------------------------------------------
#ifndef my_random_h
#define my_random_h
#include <stdio.h>
#endif /* my_random_h */
long next_random();
// ----------------------------------------------
// my_random.c
// ----------------------------------------------
#include <stdlib.h>
#include "my_random.h"
long next_random() {
return random();
}
除非您使用Swift为非苹果平台开发,否则您可以在GameplayKit中获得更好的随机化API:多种算法(交易随机性与速度)、可种子、分布控制等。对于简单的可重复随机列表,请尝试使用线性同余生成器:
import Foundation
class LinearCongruntialGenerator
{
var state = 0 //seed of 0 by default
let a, c, m, shift: Int
//we will use microsoft random by default
init() {
self.a = 214013
self.c = 2531011
self.m = Int(pow(2.0, 31.0)) //2^31 or 2147483648
self.shift = 16
}
init(a: Int, c: Int, m: Int, shift: Int) {
self.a = a
self.c = c
self.m = m //2^31 or 2147483648
self.shift = shift
}
func seed(seed: Int) -> Void {
state = seed;
}
func random() -> Int {
state = (a * state + c) % m
return state >> shift
}
}
let microsoftLinearCongruntialGenerator = LinearCongruntialGenerator()
print("Microsft Rand:")
for i in 0...10
{
print(microsoftLinearCongruntialGenerator.random())
}
更多信息请点击此处:
您可以使用
srand48(种子)和drand48()在Swift3中。我碰巧为Swift 4组合了这个。我知道Swift 4.2有了与此不同的新的随机扩展,但像OP一样,我需要它们在测试期间可播种。也许有人会发现它很有用。如果你不播种,它将使用arc4random,否则它将使用drand48。它避免了两种方式的mod偏差。
import Foundation
class Random {
static var number = unseededGenerator // the current generator
/**
* returns a random Int 0..<n
**/
func get(anIntLessThan n: Int) -> Int {
return generatingFunction(n)
}
class func set(seed: Int) {
number = seedableGenerator
srand48(seed)
}
// Don't normally need to call the rest
typealias GeneratingFunction = (Int) -> Int
static let unseededGenerator = Random(){
Int(arc4random_uniform(UInt32($0)))
}
static let seedableGenerator = Random(){
Int(drand48() * Double($0))
}
init(_ gf: @escaping GeneratingFunction) {
self.generatingFunction = gf
}
private let generatingFunction: GeneratingFunction
}
func randomTest() {
Random.set(seed: 65) // comment this line out for unseeded
for _ in 0..<10 {
print(
Random.number.get(anIntLessThan: 2),
terminator: " "
)
}
}
// Run
randomTest()
<代码>导入基础
类随机{
静态var编号=未设定发电机//当前发电机
/**
*返回一个随机整数0..Int{
返回生成函数(n)
}
类func集(种子:Int){
编号=种子发电机
srand48(种子)
}
//通常不需要打电话给其他人
typealias生成函数=(Int)->Int
静态let unseedgegenerator=Random(){
Int(arc4random_统一(UInt32($0)))
}
静态let seedableGenerator=Random(){
整数(drand48()*双倍($0))
}
init(gf:@escaping GeneratingFunction){
自生成函数=gf
}
私有let生成函数:生成函数
}
func随机测试(){
Random.set(seed:65)//注释这一行的未种子
对于0中的uu..使用arc4random()而不是srand(seed)arc4random()
不能作为种子。你确实可以得到质量更好的随机数,但如果你的要求每次都要求相同的随机数,那就不合适了。看起来你必须将代码移动到C/Obj-C。苹果要么没有及时将其移植到Beta 1,要么非常严厉地强迫你使用“好的”随机数生成器是的,但你说的好是什么意思?更高质量的一代?但无论如何,有时种子是必须的。如果苹果不提供种子生成,我会从树上掉下来。不太可预测,在生成器重复自身之前,周期更长。请参阅我的答案,了解愚蠢的C包装器。唉,框架中的桥接是不允许的:“:0:错误:不支持将桥接头与框架目标一起使用”。任何解决方法?根据@rickster的建议使用GameKit。请参阅我编辑的答案这将如何转换为stand/rand替代品?我喜欢它!只使用Int(pow(2.0,31.0))而不是Int(pow(2.0,31.0)),这既慢又不准确,因为使用双值,我更喜欢1
import Foundation
class LinearCongruntialGenerator
{
var state = 0 //seed of 0 by default
let a, c, m, shift: Int
//we will use microsoft random by default
init() {
self.a = 214013
self.c = 2531011
self.m = Int(pow(2.0, 31.0)) //2^31 or 2147483648
self.shift = 16
}
init(a: Int, c: Int, m: Int, shift: Int) {
self.a = a
self.c = c
self.m = m //2^31 or 2147483648
self.shift = shift
}
func seed(seed: Int) -> Void {
state = seed;
}
func random() -> Int {
state = (a * state + c) % m
return state >> shift
}
}
let microsoftLinearCongruntialGenerator = LinearCongruntialGenerator()
print("Microsft Rand:")
for i in 0...10
{
print(microsoftLinearCongruntialGenerator.random())
}
import Foundation
class Random {
static var number = unseededGenerator // the current generator
/**
* returns a random Int 0..<n
**/
func get(anIntLessThan n: Int) -> Int {
return generatingFunction(n)
}
class func set(seed: Int) {
number = seedableGenerator
srand48(seed)
}
// Don't normally need to call the rest
typealias GeneratingFunction = (Int) -> Int
static let unseededGenerator = Random(){
Int(arc4random_uniform(UInt32($0)))
}
static let seedableGenerator = Random(){
Int(drand48() * Double($0))
}
init(_ gf: @escaping GeneratingFunction) {
self.generatingFunction = gf
}
private let generatingFunction: GeneratingFunction
}
func randomTest() {
Random.set(seed: 65) // comment this line out for unseeded
for _ in 0..<10 {
print(
Random.number.get(anIntLessThan: 2),
terminator: " "
)
}
}
// Run
randomTest()