如何在 arc4random_uniform() 的范围之间生成随机数? [英] How does one make random number between range for arc4random_uniform()?

查看:26
本文介绍了如何在 arc4random_uniform() 的范围之间生成随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在这个代码位中的目标是随机掷两个骰子,众所周知,你的常规骰子只有 6 个面,所以我导入了 Foundation 以访问 arc4random_uniform(UInt32).我尝试使用 (1..7) 的范围来避免随机获得 0 但是返回了一个我不太喜欢的错误.我试图这样做:

dice1 = arc4random_uniform(UInt32(1..7))

但是返回了

<块引用>

找不到接受所提供参数的init"重载

我希望这是足够的信息,可以帮助您帮助我:)

请注意,我只是在操场上这样做是为了练习 swift.我不必学习如何做到这一点;在我开始构建实际应用程序之前,我只是在修补:D

//引入随机数函数进口基金会//为掷骰子创建数据存储var dice1: UInt32 = 0var dice2: UInt32 = 0//计数器变量变量 i = 0//蛇眼发生了多少次var 蛇眼 = 0//一个double被滚动了多少次var`double` = 0//掷骰子100次当我 <100{//从这里//设置掷骰子

返回'Range $T3' is not convertible to UInt32的错误

<块引用>

 dice1 = arc4random_uniform(1..7)骰子2 = arc4random_uniform(1..7)

//检查蛇眼如果骰子1 == 1 &&骰子 2 == 1 {蛇眼 = 蛇眼 + 1}//检查双打如果骰子1 == 骰子2{`double` = `double` + 1}//增加计数器我 = 我 + 1//到这里}println("你得到了 Snake Eyes (snakeeyes) 次.")println("你得到了双打,(`double`) 次.")

解决方案

我相信你应该这样做

dice1 = arc4random_uniform(6) + 1;

获得范围 1 - 6.我不做 iOS 目标 C,也没有任何关于 swift 语言的知识.random 方法应该返回 0 到 5 之间的值,+ 1 将使其成为 1 到 6 之间的值.

如果你需要一个介于 10 - 30 之间的范围,那么就做

int random = arc4random_uniform(21) + 10;

so my goal in this codebit is to randomly roll two dice and as we all know your regular die only has 6 sides so I imported Foundation for access to arc4random_uniform(UInt32). I attempted using the range of (1..7) to avoid randomly getting 0 however that returned an error which I didn't enjoy too much. I tried to do this:

dice1 = arc4random_uniform(UInt32(1..7))

however that returned

Could not find an overload for 'init' that accepts the supplied arguments

I hope that this is enough information for you amazing debs out there to help me :)

Please note I am just doing this in a playground to practice swift. It isn't imperative that I learn how to do this; it's just me tinkering before I jump into building actual apps :D

//imports random number function
import Foundation
//creates data storage for dice roll
var dice1: UInt32 = 0
var dice2: UInt32 = 0
//counter variable
var i = 0
//how many times snake eyes happens
var snakeeyes = 0
 //how many times a double is rolled
var `double` = 0
//rolls dice 100 times
while i < 100{
    //from here
    //sets dice roll

This returns an error of 'Range $T3' is not convertible to UInt32

   dice1 = arc4random_uniform(1..7)
   dice2 = arc4random_uniform(1..7)

    //checks for snake eyes
    if dice1 == 1 && dice2 == 1 {
        snakeeyes = snakeeyes + 1

    }
    //checks for doubles
    if dice1 == dice2{
        `double` = `double` + 1
    }
    //increases counter
        i = i + 1
    //to here
}
println("You got Snake Eyes (snakeeyes) times.")
println("You got Doubles, (`double`) times.")

解决方案

I believe you should do

dice1 = arc4random_uniform(6) + 1;

to get the range 1 - 6. I don't do iOS objective C nor have I any knowledge on swift-language though. The random method should return a value between 0 and 5, and + 1 will make it a value between 1 and 6.

If you need a range between lets say 10 - 30 then just do

int random = arc4random_uniform(21) + 10;

这篇关于如何在 arc4random_uniform() 的范围之间生成随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆