- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现以下算法,详细信息为 here .
Start with a flat terrain (initialize all height values to zero).
Pick a random point on or near the terrain, and a random radius between some predetermined minimum and maximum. Carefully choosing this min and max will make a terrain rough and rocky or smooth and rolling.
Raise a hill on the terrain centered at the point, having the given radius.
Go back to step 2, and repeat as many times as necessary. The number of iterations chosen will affect the appearance of the terrain.
但是,一旦我必须在地形上选择一个随机点,我就开始挣扎。这个随机点被包装在一个 IO monad 中,然后沿着我的函数链向上传递。
我可以在某个点切断IO
吗?如果可以,我如何找到该点?
以下是我的(损坏的)代码。我将不胜感激任何关于改进它/阻止随机性感染一切的建议。
type Point = (GLfloat, GLfloat, GLfloat)
type Terrain = [Point]
flatTerrain :: Double -> Double -> Double -> Double -> Terrain
flatTerrain width length height spacing =
[(realToFrac x, realToFrac y, realToFrac z)
| x <- [-width,-1+spacing..width], y <- [height], z <- [-length,-1+spacing..length]]
hill :: Terrain -> Terrain
hill terrain = hill' terrain 100
where hill' terrain 0 = terrain
hill' terrain iterations = do
raised <- raise terrain
hill' (raise terrain) (iterations - 1)
raise terrain = do
point <- pick terrain
map (raisePoint 0.1 point) terrain
raisePoint r (cx,cy,cz) (px,py,pz) =
(px, r^2 - ((cx - px)^2 + (cz - pz)^2), pz)
pick :: [a] -> IO a
pick xs = randomRIO (0, (length xs - 1)) >>= return . (xs !!)
最佳答案
该算法表示您需要迭代,并在每次迭代中选择一个随机数并更新地形
,这可以视为生成随机点列表并使用此列表进行更新地形
即迭代生成随机数==随机数列表。
所以你可以这样做:
selectRandomPoints :: [Points] -> Int -> IO [Points] -- generate Int times random points
updateTerrain :: Terrain -> [Points] -> Terrain
-- somewhere in IO
do
pts <- selectRandomPoints allPts iterationCount
let newTerrain = updateTerrain t pts
关于haskell - 如何阻止随机性渗透到 Haskell 中的代码中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19994920/
我试图影响 Z3 生成的模型值结果的随机性。据我所知,这方面的选择非常有限:在线性算术的情况下,单纯形求解器不允许仍然满足给定约束的随机结果。但是,有一个选项 smt.arith.random_ini
我需要从表中获取一些数据并通过两个参数对它们进行排序。参数之一是 RAND(),因为记录需要是随机的。我有一个很大的数据库,使用 RAND() 会大大降低性能,我想避免它。是否有机会优化随机排序的性能
所以我尝试将 Boost.Random mt19937 生成器与模板一起使用。我的 c++ 有点生疏,但据我所知(和文档一样,Boost 的文档也很模糊)它应该采用一个模板参数来指定它的返回类型(fl
我正在尝试为家庭作业编写一个遗传算法来解决旅行商问题。 我正在尝试的变异函数之一是在游览中使用 random.shuffle。 当我阅读 random.shuffle 的文档时,我看到: shuffl
尝试创建随机 (0/1) boolean 操作。我设置了一个多维数组。到目前为止,一切正常。我在正确的道路上吗?我目前使用递归;有没有更有效的方法来做到这一点? function randomMove
我想在每天下午 1 点到 2 点之间的随机时间运行一个云函数。我使用 Cloud Scheduler (cron) 将消息发布到触发该功能的 PubSub。我想在 Cloud Scheduler 端实
我想在每天下午 1 点到 2 点之间的随机时间运行一个云函数。我使用 Cloud Scheduler (cron) 将消息发布到触发该功能的 PubSub。我想在 Cloud Scheduler 端实
这可能是一个相当愚蠢的问题。我想知道在 Swift 中使用 arc4random_uniform 是否可以实现不同的细微差别/随机性程度。这是一个例子: let number = arc4random
我正在开发一部分代码,其中我必须使用日历 API 使用现有的 api,而我使用的是全新的 API。在转换中出现了一些奇怪的行为,请看这个例子: SimpleDateFormat df = new Si
我是一名优秀的程序员,十分优秀!