我在Ubuntu中使用Swift,我收到一个错误,arc4random是一个未解析的标识符.在这个已知的错误的详细信息在这里.基本上,该功能仅存在于BSD发行版中.我已经尝试过模块映射头文件,apt-getting包,并且我得到越来越多的错误,这是不值得追求的,因为这个函数不经常使用.
是否有任何函数可以获取具有与Linux中的Swift兼容的上限参数的伪随机数?
我需要一个函数来生成随机整数.(long现在假设Java 类型,但这将扩展到BigInteger或BitSet稍后.)
棘手的部分是有一个参数P,它指定结果中任何位的(独立)概率为1.
如果P = 0.5,那么我们可以使用标准随机数发生器.P的一些其他值也易于实现.这是一个不完整的例子:
Random random = new Random();
// ...
long nextLong(float p) {
if (p == 0.0f) return 0L;
else if (p == 1.0f) return -1L;
else if (p == 0.5f) return random.nextLong();
else if (p == 0.25f) return nextLong(0.5f) & nextLong(0.5f);
else if (p == 0.75f) return nextLong(0.5f) | nextLong(0.5f);
else if (p == 0.375f) return nextLong(0.5f) & nextLong(0.75f); // etc
else {
// What goes here??
String message …Run Code Online (Sandbox Code Playgroud)