# Pseudorandom Number Generator

### Example 1

```javascript
(module
  (global $seed (import "js" "global") (mut i32))
  (func (export "rand") (result i32)
  (local $s i32)
  (i32.rem_s 
    (i32.add 
      (i32.mul 
        (i32.const 1103515245) 
        (get_global $seed)
      )  
      (i32.const 12345)
    )
    (i32.const 2147483648)
  )
  tee_local $s
  get_local $s
  set_global $seed
))
```

### Example 2

```javascript
(module
  (func $pow (export "pow") (param $x f32) (param $y f32) (result f32)
    (local $out f32)
    (local $index f32)
    f32.const 1
    set_local $out
    f32.const 1
    set_local $index

    (block $b0
      (loop $l0
        (f32.mul 
          (get_local $out)
          (get_local $x)
        )
        set_local $out

        (f32.add 
          (get_local $index)
          (f32.const 1)
        )
        tee_local $index
        get_local $y
        f32.gt
        br_if 1

        br 0
      )
    )

    get_local $out
  )
  (func $sin (export "sin") (param $x f32) (result f32)
    (f32.sub
      (f32.add
        (f32.sub
          (get_local $x)
          (f32.div 
            (call $pow (get_local $x) (f32.const 3))
            (f32.const 6)
          )
        )
        (f32.div 
          (call $pow (get_local $x) (f32.const 5))
          (f32.const 120)
        )
      )
      (f32.div 
          (call $pow (get_local $x) (f32.const 7))
        (f32.const 5040)
      )
    )
  )
  (func $fract (export "fract") (param $x f32) (result f32)
    (f32.sub
      (get_local $x)
      (f32.floor (get_local $x))
    )
  )
  (func $rand (export "rand") (param $x f32) (result f32)
    (f32.mul 
      (call $sin (get_local $x))
      (f32.const  43758.5453123)
    )
    call $fract
  )
)

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://augustus-pash.gitbook.io/wasm/maths-algorithms/pseudorandom-number-generator.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
