Jump to content

How to add a SEED into your own function


Recommended Posts

To add a SEED to your own function and make it work you need to follow three simple rules:

  1. Add keyword SEED into the function arguments.
  2. (rnd-seed seed) should be placed at the beginning of the function.
  3. Nested functions that use seed need to use a (seed) function and not a seed argument.

Example:

(defun rnd-number (n low high &key seed)
  (do-verbose ("rnd-number")
    (rnd-seed seed)
    (if (zerop n)
        nil
        (cons (round (+ low (rnd-round 0 (- high low) :seed (seed))))
              (rnd-number (decf n) low high :seed (seed))))))

 

Each time we evaluate the expression we get a different result.

(rnd-number 12 0 5)
=> (0 3 2 5 3 3 4 3 2 2 4 4)

 

Adding seed to the expression will produce always the same result.

(rnd-number 12 0 5 :seed 45)
=> (2 2 5 4 5 0 5 3 5 3 1 1)
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

Terms of Use Privacy Policy