• Top
    • Documentation
    • Books
    • Recursion-and-induction
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
      • Theories
      • Rule-classes
      • Proof-builder
      • Hons-and-memoization
      • Events
      • History
      • Parallelism
      • Programming
        • Defun
        • Declare
        • System-utilities
        • Stobj
        • State
          • World
          • Io
          • Wormhole
          • Programming-with-state
          • W
          • Set-state-ok
          • Random$
            • Seed-random$
            • Random-list-aux
            • Random$-lemmas
            • Random-list
        • Memoize
        • Mbe
        • Io
        • Defpkg
        • Apply$
        • Mutual-recursion
        • Loop$
        • Programming-with-state
        • Arrays
        • Characters
        • Time$
        • Loop$-primer
        • Fast-alists
        • Defmacro
        • Defconst
        • Evaluation
        • Guard
        • Equality-variants
        • Compilation
        • Hons
        • ACL2-built-ins
        • Developers-guide
        • System-attachments
        • Advanced-features
        • Set-check-invariant-risk
        • Numbers
        • Irrelevant-formals
        • Efficiency
        • Introduction-to-programming-in-ACL2-for-those-who-know-lisp
        • Redefining-programs
        • Lists
        • Invariant-risk
        • Errors
        • Defabbrev
        • Conses
        • Alists
        • Set-register-invariant-risk
        • Strings
        • Program-wrapper
        • Get-internal-time
        • Basics
        • Packages
        • Defmacro-untouchable
        • Primitive
        • <<
        • Revert-world
        • Set-duplicate-keys-action
        • Unmemoize
        • Symbols
        • Def-list-constructor
        • Easy-simplify-term
        • Defiteration
        • Defopen
        • Sleep
      • Start-here
      • Real
      • Debugging
      • Miscellaneous
      • Output-controls
      • Macros
      • Interfacing-tools
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
    • Testing-utilities
    • Math
  • State
  • Numbers
  • ACL2-built-ins

Random$

Obtain a random value

Example:
(random$ 10 state) ==> (mv 7 <state>)

(Random$ limit state), where limit is a positive integer, returns a random non-negative integer together with a new state. Logically, it simply returns the first element of a list that is a field of the ACL2 state, called the acl2-oracle (see read-ACL2-oracle), together with the new state resulting from removing that element from that list. (Except, if that element is not in range as specified above, then 0 is returned.) However, random$ actually invokes a Common Lisp function to choose the integer returned. Quoting from the Common Lisp HyperSpec(TM), http://www.lispworks.com/documentation/HyperSpec/Front: ``An approximately uniform choice distribution is used... each of the possible results occurs with (approximate) probability 1/limit.''

Consider enabling rules natp-random$ and random$-linear if you want to reason about random$.

Function: random$

(defun random$ (limit state)
       (declare (type (integer 1 *) limit)
                (xargs :stobjs state))
       (mv-let (erp val state)
               (read-acl2-oracle state)
               (mv (cond ((and (null erp)
                               (natp val)
                               (< val limit))
                          val)
                         (t 0))
                   state)))

Subtopics

Seed-random$
Influence the random numbers produced by random$.
Random-list-aux
Add random numbers onto an accumulator.
Random$-lemmas
Lemmas about random$ available in the system/random book.
Random-list
Generate a list of random numbers in [0, limit).