本文介绍了如何使用rand-int生成可重复的随机序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Clojure中使用 rand 生成可重复的数字。 (具体来说,我想要调用 rand-nth 或Incanter的示例的结果是可重复的,这些调用 rand-int ,它会调用 rand )。

I want to be able to generate repeatable numbers using rand in Clojure. (Specifically, I want results of calls to rand-nth or Incanter's sample to be repeatable, and these call rand-int which in turn calls rand).

从听起来好像可能是相同的问题,但它要求完全不同的东西。)

(Another question sounds as if it might be the same issue, but it's asking about something completely different.)

推荐答案

可能不是最干净的方法,但你可以通过重新定义 clojure.core / rand

Probably not the cleanest way, but you can make it work by redefining clojure.core/rand:

(ns clojure.core)

(def r (java.util.Random. 1))

(defn rand
  ([] (.nextDouble r))
  ([n] (.nextInt r n)))

(take 10 (repeatedly #(rand-int 10)))

每次运行时会产生(5 8 7 3 4 4 4 6 8 8)。

This produces (5 8 7 3 4 4 4 6 8 8) every time I run it.

这篇关于如何使用rand-int生成可重复的随机序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 04:29
查看更多