Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 在F中创建一个随机小数#_F#_Record - Fatal编程技术网

F# 在F中创建一个随机小数#

F# 在F中创建一个随机小数#,f#,record,F#,Record,我试图创建一个随机小数,如下所示: type Fraction= { num: int den: int } let makeRandomFraction i = let n= fun i -> System.Random(i).Next(-50, 51) let d= fun i -> System.Random(i*3).Next(-50, 51) {num=n; den=d} 但我得到了一个错误: error FS0001: This expre

我试图创建一个随机小数,如下所示:

type Fraction=
  {
  num: int
  den: int
  }

let makeRandomFraction i =
  let n= fun i -> System.Random(i).Next(-50, 51)
  let d= fun i -> System.Random(i*3).Next(-50, 51)
  {num=n; den=d}
但我得到了一个错误:

error FS0001: This expression was expected to have type
    'int'
but here has type
    'int -> int'

您能解释一下错误和正确的执行方法吗。

错误是指您正在传递一个函数(类型为
int->int
),其中需要
int
。您可以在以下内容中更清楚地看到这一点:

let add a b = a + b
let inc x = x + 1

inc add
//  ^^^
// This expression was expected to have type 'int' but has type 'int -> int -> int'

这里的解决方案就是取出
fun i->
部件。这是lambdas的(其他)语法,但是由于
i
变量已经在作用域中,因此不需要在其周围创建函数。

错误是,您正在传递一个函数(类型为
int->int
),其中需要
int
)。您可以在以下内容中更清楚地看到这一点:

let add a b = a + b
let inc x = x + 1

inc add
//  ^^^
// This expression was expected to have type 'int' but has type 'int -> int -> int'

这里的解决方案就是取出
fun i->
部件。这是lambdas的(其他)语法,但由于
i
变量已经在作用域中,因此不需要围绕它创建函数。

出于好奇,您是否对函数要生成的分数的范围和分布有任何要求?当前编写代码的方式-通过在-50范围内合成两个随机数。。50,您将得到一个分布,其中大多数数字接近于零

下面是使用XPlot F#库构建的简单直方图:


出于好奇,您是否对要生成的分数的范围和分布有任何要求?当前编写代码的方式-通过在-50范围内合成两个随机数。。50,您将得到一个分布,其中大多数数字接近于零

下面是使用XPlot F#库构建的简单直方图:


谢谢,实际上我想要两个数字的比率,都在-50到50之间。谢谢,实际上我想要两个数字的比率,都在-50到50之间。