Random 生成随机ASCII字符

Random 生成随机ASCII字符,random,scheme,lisp,ascii,Random,Scheme,Lisp,Ascii,我需要在scheme中生成一个随机的ASCII字母(大写或小写)或字符(但不是数字),我想知道这样做的合适方法是什么。目前我已经有了密码 (define a 1) (define b 16) (define (s8 a b) (when (<= a b) (if (= (mod a 8) 0) (write "h") (write a)) (s8 (+ a 1) b))) (s8 a b) 一种简单的方法是将所有可接受的字符放在一个列表中,然后

我需要在scheme中生成一个随机的ASCII字母(大写或小写)或字符(但不是数字),我想知道这样做的合适方法是什么。目前我已经有了密码

(define a 1)
(define b 16)
(define (s8 a b)
  (when (<= a b)
    (if (= (mod a 8) 0)
      (write "h")
      (write a))
    (s8 (+ a 1) b)))
(s8 a b)

一种简单的方法是将所有可接受的字符放在一个列表中,然后从中随机选择一个:

(define random-letter
        ; this is to avoid redefining the list of acceptable letters every time
  (let* ((chars '("a" "e" "i" "o" "u"))
        ; and this is to pre-calculate the length
         (len (length chars))) 
    (lambda () ; the actual procedure, it doesn't require arguments
      (list-ref chars (random len))))) ; pick a random index from list
确保在列表中添加所有需要的字符。使用此过程非常简单,如下所示:

(random-letter)
=> "u"

以下是如何在工业强度方案派生语言中实现这一点:特别是Racket。它具有各种功能,这些功能 您可能需要在更简单的方案中实现,例如函数 生成字符串、在字符和整数之间转换以及PRNG (还有咖喱)

如果您的方案缺少一些这方面的功能,您可能必须编写它,这在教育上可能很有趣,但在其他方面就没有了

(define latin-alpha-string
  ;; This assumes that a-z are a sequence of adjacent, increasing
  ;; character codes, as are A-Z, but nothing about their relative values
  (let ([lca (char->integer #\a)]
        (uca (char->integer #\A)))
    ;; build-string makes a string by calling a function which takes an index
    ;; and returns the character at that index.
    (build-string (+ 26 26)
                  (λ (i)
                    (integer->char
                     (if (< i 26)
                         (+ lca i)
                         (+ uca (- i 26))))))))

(define (random-string-char s (rand random))
  ;; The general case: return a random element of a string.  rand, if provided,
  ;; should be a function of one argument, i, which returns a (pseudo-)random
  ;; integer in [0, i).
  (string-ref s (rand (string-length s))))

(define random-latin-alpha-char
  ;; and here's a curried version of the above for latin-alpha strings
  (curry random-string-char latin-alpha-string))
(定义拉丁字母字符串
这假设a-z是一个相邻的、递增的序列
字符代码,如A-Z,但与它们的相对值无关
(让([lca(字符->整数#\a)]
(uca(字符->整数#\A)))
;build string通过调用具有索引的函数来生成字符串
;并返回该索引处的字符。
(构建字符串(+26)
(λ(i)
(整数->字符
(如果(
不要重新发明轮子!Scheme有生成随机值的内置程序。我将定义放在开头,然后放入
(随机字母)
而不是
(写“h”)
并给出错误
1234567错误:执行:未绑定符号:“随机”[s8、s8、s8、s8、s8、s8、随机字符]
。这意味着您的解释器没有
随机过程或名称不同。您使用的解释器是什么?请查看文档以找到正确的过程,或者下载Racket,这对学习非常有帮助。此外,我不确定您是如何执行代码的,显示的输出会没有意义-你应该清除所有内容,然后复制粘贴上面的代码片段。我使用的是在线解释器repl.it。上面写着“BiwaScheme解释器版本0.6.4版权(C)2007-2014 Yutaka HARA和BiwaScheme团队”。我用我运行的代码编辑。在学习Scheme时,与在线编辑器合作不是一个好主意,不是所有人都能实现整个标准,某些功能和/或过程将丢失。帮自己一个忙,下载Racket;)不仅仅是解释器没有随机性,Scheme也没有用它定义。有一个是运行了
的解释器dom整数
随机实数
(random-letter)
=> "u"
(define latin-alpha-string
  ;; This assumes that a-z are a sequence of adjacent, increasing
  ;; character codes, as are A-Z, but nothing about their relative values
  (let ([lca (char->integer #\a)]
        (uca (char->integer #\A)))
    ;; build-string makes a string by calling a function which takes an index
    ;; and returns the character at that index.
    (build-string (+ 26 26)
                  (λ (i)
                    (integer->char
                     (if (< i 26)
                         (+ lca i)
                         (+ uca (- i 26))))))))

(define (random-string-char s (rand random))
  ;; The general case: return a random element of a string.  rand, if provided,
  ;; should be a function of one argument, i, which returns a (pseudo-)random
  ;; integer in [0, i).
  (string-ref s (rand (string-length s))))

(define random-latin-alpha-char
  ;; and here's a curried version of the above for latin-alpha strings
  (curry random-string-char latin-alpha-string))