PKUOS - Pintos
Pintos source browser for PKU Operating System course
Functions | Variables
random.c File Reference
#include "random.h"
#include <stdbool.h>
#include <stdint.h>
#include "debug.h"
Include dependency graph for random.c:

Go to the source code of this file.

Functions

static void swap_byte (uint8_t *a, uint8_t *b)
 Swaps the bytes pointed to by A and B. More...
 
void random_init (unsigned seed)
 Initializes or reinitializes the PRNG with the given SEED. More...
 
void random_bytes (void *buf_, size_t size)
 Writes SIZE random bytes into BUF. More...
 
unsigned long random_ulong (void)
 Returns a pseudo-random unsigned long. More...
 

Variables

static uint8_t s [256]
 RC4-based pseudo-random number generator (PRNG). More...
 
static uint8_t s_i
 
static uint8_t s_j
 i, j. More...
 
static bool inited
 Already initialized? More...
 

Function Documentation

◆ random_bytes()

void random_bytes ( void *  buf_,
size_t  size 
)

Writes SIZE random bytes into BUF.

Definition at line 54 of file random.c.

References buf, inited, random_init(), s, s_i, s_j, and swap_byte().

Referenced by main(), random_ulong(), seq_test(), and test_main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ random_init()

void random_init ( unsigned  seed)

Initializes or reinitializes the PRNG with the given SEED.

Definition at line 34 of file random.c.

References inited, s, s_i, s_j, and swap_byte().

Referenced by consume_some_resources_and_die(), main(), parse_options(), random_bytes(), and test_main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ random_ulong()

unsigned long random_ulong ( void  )

Returns a pseudo-random unsigned long.

lib/random.h

Use random_ulong() % n to obtain a random number in the range 0...n (exclusive).

Definition at line 78 of file random.c.

References random_bytes().

Referenced by consume_some_resources_and_die(), expand(), pick_pivot(), shuffle(), test(), and write_some_bytes().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ swap_byte()

static void swap_byte ( uint8_t a,
uint8_t b 
)
inlinestatic

Swaps the bytes pointed to by A and B.

Definition at line 25 of file random.c.

Referenced by random_bytes(), and random_init().

Here is the caller graph for this function:

Variable Documentation

◆ inited

bool inited
static

Already initialized?

Definition at line 21 of file random.c.

Referenced by init(), random_bytes(), and random_init().

◆ s

uint8_t s[256]
static

RC4-based pseudo-random number generator (PRNG).

RC4 is a stream cipher. We're not using it here for its cryptographic properties, but because it is easy to implement and its output is plenty random for non-cryptographic purposes.

See http://en.wikipedia.org/wiki/RC4_(cipher) for information on RC4. RC4 state. S[].

Definition at line 17 of file random.c.

Referenced by __vprintf(), arc4_crypt(), arc4_init(), atoi(), cksum(), hash_string(), parse_octal_field(), puts(), random_bytes(), random_init(), shutdown_power_off(), strtok_r(), and udiv64().

◆ s_i

uint8_t s_i
static

Definition at line 18 of file random.c.

Referenced by random_bytes(), and random_init().

◆ s_j

uint8_t s_j
static

i, j.

Definition at line 18 of file random.c.

Referenced by random_bytes(), and random_init().