Skip to content

Commit

Permalink
update std/math/rand
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Oct 1, 2024
1 parent 5b9479f commit ee75edd
Showing 1 changed file with 45 additions and 48 deletions.
93 changes: 45 additions & 48 deletions src/std/math-rand.md
Original file line number Diff line number Diff line change
@@ -1,75 +1,72 @@
# std/math/rand
## Type Aliases
```jule
type Seed: u64
```
Integer type of random seeds.

## Structures
## Functions

```jule
struct Rand
fn NewSource(seed: u64): Source
```
This structure implements a type of pseudo random number generator (PRNG). The seed must be given manually for each instance.
Returns new default source by seed.

Seeds:
<ul>
Seed is a number of seeds used to simulate randomness. The order and numbers produced vary depending on the seed. Since PRNGs are inherently deterministic, using a fixed seed means your program will generate the same numbers every time.
The order and numbers produced vary depending on the seed. Since PRNGs are inherently deterministic, using a fixed seed means your program will generate the same numbers every time.

If you want to achieve randomness somehow, use a variable seed. A simple solution for seeds that will create the illusion of randomness is to use time. Unix-time seconds could be a simple seed solution.
</ul>
If you want to achieve randomness somehow, use a variable seed. A simple solution for seeds that will create the illusion of randomness is to use time. Unix-time seconds would be a simple seed solution.

Ranges:
<ul>
This structure by default provides a function to simulate randomness between two numbers. But a way to do this can be suggested.
## Structs

Here is basic range formula: \
&ensp;&ensp;`ƒ(x) -> rand.Nextn(max - min) + min`

With this formula, randomness can be made in the `[min, max)` range.
</ul>
```jule
struct Rand
```
Implements a type of pseudo random number generator (PRNG). Outputs might be easily predictable regardless of how it's seeded. For random numbers suitable for security-sensitive work, it is not recommended.

**Methods:**

`static fn New(seed: Seed): &Rand`\
Returns new PRNG for seed.

---
`static fn New(src: Source): &Rand`\
Returns new `Rand` that uses random values from `src` to generate other random values.

`fn Next63(self): i64`\
Returns a non-genative pseudo-random 63-bit signed integer as an 64-bit signed integer.
`fn U64(self): u64`\
Returns a pseudo-random 64-bit value as `u64`.

---
`fn I64(self): u32`\
Returns a pseudo-random 32-bit value as `u32`.

`fn Next31(self): i32`\
Returns a non-genative pseudo-random 31-bit signed integer as an 31-bit signed integer.
`fn I64(self): i64`\
Returns a non-negative pseudo-random 64-bit value as `i64`.

---
`fn I32(self): i32`\
Returns a non-negative pseudo-random 32-bit value as `i32`.

`fn Next(self): int`\
Returns a non-genative pseudo-random int.
`fn Int(self): int`\
Returns a non-negative pseudo-random int.

---
`fn U64n(self, n: u64): u64`\
Returns, as a `u64`, a non-negative pseudo-random number in the half-open interval `[0,n)`. It panics if `n == 0`.

`fn Nextn63(self, n: i64): i64`\
Returns a non-genative pseudo-random in `[0, n)` range 63-bit signed integer as an 64-bit signed integer. If `n <= 0`, it panics.
`fn I64n(self, n: i64): i64`\
Returns, as an `i64`, a non-negative pseudo-random number in the half-open interval `[0,n)`. It panics if `n == 0`.

---
`fn I32n(self, n: i32): i32`\
Returns, as an `i32`, a non-negative pseudo-random number in the half-open interval `[0,n)`. It panics if `n <= 0`.

`fn Nextn31(self, n: i32): i32`\
Returns a non-genative pseudo-random in `[0, n)` range 31-bit signed integer as an 31-bit signed integer. If `n <= 0`, it panics.
`fn U32n(self, n: u32): u32`\
Returns, as a `u32`, a non-negative pseudo-random number in the half-open interval `[0,n)`. It panics if `n == 0`.

---
`fn Intn(self, n: int): int`\
Returns, as an `int`, a non-negative pseudo-random number in the half-open interval `[0,n)`. It panics if `n <= 0`.

`fn Nextn(self, n: int): int`\
Returns a non-genative pseudo-random in [0, n) range as int. If `n <= 0`, it panics.
`fn Uintn(self, n: uint): uint`\
Returns, as a `uint`, a non-negative pseudo-random number in the half-open interval `[0,n)`. It panics if `n == 0`.

---
`fn F64(self): f64`\
Returns, as a `f64`, a pseudo-random number in the half-open interval `[0.0,1.0)`.

`fn Fnext64(self): f64`\
Returns a non-genative pseudo-random in `[0.0, 1.0)` range as f64 floating-point.
`fn F32(self): f32`\
Returns, as a `f32`, a pseudo-random number in the half-open interval `[0.0,1.0)`.

---
## Traits

`fn Fnext32(self): f32`\
Returns a non-genative pseudo-random in `[0.0, 1.0)` range as f32 floating-point.
```jule
trait Source {
fn U64(self)
}
```
Source of uniformly-distributed pseudo-random u64 values in the range `[0, 1<<64)`. It is not safe for concurrent use by multiple threads.

0 comments on commit ee75edd

Please sign in to comment.