0

我得到一个介于 0 和 1 之间的实数。这是我的友善因素。这个因素是一种概率。我的意思是如果它是 0.2,这意味着某事将以 1/5 的概率发生。

我试着用

random 

为此的程序。这是我的代码片段:

  (if (eq? 'c (tit-for-tat my-history other-history))
   'c
   (if (= (random (/ 1 niceness-factor)) 0) 'c 'd))

然而,“随机”程序要我给出一个确切的整数。我怎么解决这个问题?有没有类似随机的程序?

提前致谢...

4

3 回答 3

1

只需将您niceness-factor的值转换为类似整数的值(与 的结果相当random)。例如:

(if (eq? 'c (tit-for-tat my-history other-history))
    'c
    (if (> (random 100) (* 100 niceness-factor)) 
        'c 
        'd)))
于 2013-05-04T19:20:11.927 回答
0

In the modules racket and racket/base (random k) generates an integer from 0 to k-1 inclusive. (k must be between 1 and 4294967087 inclusive) (random gives an inexact number between 0 and 1 exclusive. reference guide: random provided by racket and racket/base

Assuming you are using the racket, and not one of the other languages racket supports, from where you started adding a call to exact-round would be closest to what you started with:

(if (eq? 'c (tit-for-tat my-history other-history))
    'c
    (if (= (random (exact-round (/ 1 niceness-factor))) 0) 'c 'd))

But I find the following to be simpler:

(if (eq? 'c (tit-for-tat my-history other-history))
    'c
    (if (<= (random) niceness-factor) 'c 'd))
于 2013-05-07T00:44:40.140 回答
0

解决这个问题的一种方法是使用(随机 1)。现在,如果结果 <= niceness-factor 然后做“某事”,否则做“什么都不做”。

理由:

有 20% 的概率在 (0,1) 范围内获得一个 <=0.2 的数字。

于 2013-05-04T18:36:33.887 回答