Recursion 在F#中创建随机数时如何避免使用特定数字?
我试图为每个方向生成一个从0到3的随机数-n,e,s,w。无论递归函数中的前一个方向是什么,都不能再使用,我正试图找出实现这一点的最佳方法。我还希望避免使用可变项。下面是我到目前为止所做的,它将起作用,但我相信有更好的方法来做到这一点Recursion 在F#中创建随机数时如何避免使用特定数字?,recursion,random,f#,Recursion,Random,F#,我试图为每个方向生成一个从0到3的随机数-n,e,s,w。无论递归函数中的前一个方向是什么,都不能再使用,我正试图找出实现这一点的最佳方法。我还希望避免使用可变项。下面是我到目前为止所做的,它将起作用,但我相信有更好的方法来做到这一点 open System let width = Console.WindowWidth let height = Console.WindowHeight let (map: int[,]) = Array2D.zeroCreate width height l
open System
let width = Console.WindowWidth
let height = Console.WindowHeight
let (map: int[,]) = Array2D.zeroCreate width height
let random = Random()
let main x y d =
let mutable nd = random.Next(4)
while nd = d do
nd <- random.Next(4)
开放系统
let width=Console.WindowWidth
let height=控制台高度。窗口高度
let(map:int[,])=Array2D.zeroCreate-width-height
设random=random()
让main x y d=
设可变nd=random.Next(4)
当nd=d时
nd您只需要三个值中的一个:
let nd1 = random.Next 3
let nd = if nd1 >= d then nd1 + 1 else nd1
如果我决定使用8个方向怎么办?只需将3
替换为7
,完全相同的代码就可以了。