Scheme - generate random

How to generate random data in a circuit? Is there a special form or do I need to create a procedure? And if so, how do I do this? (I'm trying to create a procedure called random selection that introduces two strategies and returns them randomly.)

+5
source share
3 answers

The procedure is called, oddly enough, randomalthough the exact syntax may vary depending on the interpreter used (see the documentation!), But the general idea is this:

(random)
=> 0.9113789707345018

To return one of two possible values, this will do the trick in Racket:

(define (random-choice a b)
  (if (zero? (random 2)) a b))

, 2, random, : 0 1. , (random 2) 0, a, b.

(random-choice 4 2)
=> 4
(random-choice 4 2)
=> 2
+5

, , , , , . Scheme, ; - :

(define random
  (let ((a 69069) (c 1) (m (expt 2 32)) (seed 19380110))
    (lambda new-seed
      (if (pair? new-seed)
          (set! seed (car new-seed))
          (set! seed (modulo (+ (* seed a) c) m)))
      (/ seed m))))

(random) 0 () 1 (). m. (random seed) , , , ; YYYYMMDD ( ). , : (if (< (random) 1/2) 'heads 'tails).

. randint, , lo () hi (); lo 0:

(define (randint . args)
  (cond ((= (length args) 1)
          (floor (* (random) (car args))))
        ((= (length args) 2)
          (+ (car args) (floor (* (random) (- (cadr args) (car args))))))
        (else (error 'randint "usage: (randint [lo] hi)"))))

, , , , . , , , .

+6

DrRacket, , Scheme DrRacket.

DrRacket . , , random . , F1.

random htdp- :

http://docs.racket-lang.org/htdp-langs/beginner.html?q=random# (def.htdp-beginner. ( (lib._lang/HTDP-beginner..rkt)._ ))

:

(list-ref (list "one" "two") (random 2))

( 2) 0 1. , -ref 0 1 .

, .

+1
source