~donmcc/lrnd

fd8d136ba48e3e56263927beeb99e3c03a393011 — Don McCaughey 4 months ago master
Created initial project skeleton.

Added header and source files `lrnd.h` and `lrnd.c` respectively.
Filled them in with an initial definition of the `lrnd` struct and the
`lrnd_next_value()` function.  Also defined constant `lrnd_stdlib_rand`
generator that wraps the standard library `rand()` function.

Added initial test file `lrnd_tests.c` with a simple test.  Created an
Autotools build system.  Created a readme file and man page.  Added a
BSD-style license.
9 files changed, 218 insertions(+), 0 deletions(-)

A .gitignore
A LICENSE
A Makefile.am
A README.md
A configure.ac
A lrnd.3
A lrnd.c
A lrnd.h
A lrnd_tests.c
A  => .gitignore +11 -0
@@ 1,11 @@
.DS_Store
aclocal.m4
autoscan.log
autom4te.cache
build-aux
config.h.in
configure
configure.scan
Makefile.in
tmp


A  => LICENSE +24 -0
@@ 1,24 @@
lrnd
Copyright (c) 2020, Don McCaughey. All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this 
  list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice, 
  this list of conditions and the following disclaimer in the documentation 
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


A  => Makefile.am +14 -0
@@ 1,14 @@
AM_CPPFLAGS = -D_GNU_SOURCE
AM_CFLAGS = -std=c99

lib_LIBRARIES = liblrnd.a
include_HEADERS = lrnd.h
liblrnd_a_SOURCES = lrnd.c

LDADD = $(lib_LIBRARIES)
check_PROGRAMS = lrnd_tests
TESTS = $(check_PROGRAMS)

dist_man3_MANS = lrnd.3
EXTRA_DIST = LICENSE README.md


A  => README.md +42 -0
@@ 1,42 @@
# lrnd

A C99 library for generating random numbers.

[![builds.sr.ht status](https://builds.sr.ht/~donmcc/lrnd.svg)](https://builds.sr.ht/~donmcc/lrnd?)

## Overview

The *lrnd* library contains functions for generating random numbers using 
pluggable random number generators.

## Structs

    struct lrnd {
        uint32_t const max_value;
        bool (*next_value)(uint32_t *value_out);
    };

The `max_value` field contains the _inclusive_ maximum value that the generator
can produce.
The `next_value` field contains a pointer to a function that generates the 
next random value for this generator.

## Functions

    struct lrnd *
    lrnd_get_stdlib_rand(void);

Retrieve a static instance of the `rand()` pseudo-random number generator.

    bool
    lrnd_next_value(struct lrnd *lrnd, uint32_t *value_out);

Generate the next random value for the given generator.  Returns `false` and
sets `errno` if generation fails.

## License
`lrnd` is made available under a BSD-style license; see the [LICENSE][1] file
for details.

[1]: https://git.sr.ht/~donmcc/lrnd/tree/master/LICENSE


A  => configure.ac +30 -0
@@ 1,30 @@
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
AC_INIT([lrnd], [0.1], [don@donm.cc])
AC_CONFIG_AUX_DIR([build-aux])

AM_INIT_AUTOMAKE([1.14 foreign subdir-objects -Wall -Werror])

AC_CONFIG_SRCDIR([lrnd.h])

# Checks for programs.
AM_PROG_AR
AC_PROG_CC_C99
AC_PROG_INSTALL
AC_PROG_MKDIR_P
AC_PROG_RANLIB

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([Makefile])

AC_OUTPUT


A  => lrnd.3 +27 -0
@@ 1,27 @@
.Dd May 26, 2020
.Dt LRND 3
.Os
.Sh NAME
.Nm lrnd ,
.Nm lrnd_stdlib_rand ,
.Nm lrnd_next_value()
.Nd pluggable random number generation functions.
.Sh SYNOPSIS
.In lrnd.h
.Ft bool
.Fn lrnd_next_value "struct lrnd *lrnd" "uint32_t *value_out"
.Sh DESCRIPTION
.Fn lrnd_next_value
generates the next random value from the given generator.
.Sh RETURN VALUES
.Fn lrnd_next_value
returns
.Dv true
on sucess and places the next value in the location pointed to by
.Fa value_out .
On failure, the function returns
.Dv false
and sets
.Va errno .
.Sh SEE ALSO
.Xr rand 3

A  => lrnd.c +25 -0
@@ 1,25 @@
#include "lrnd.h"

#include <stdlib.h>


static bool
stdlib_rand_next_value(uint32_t *value_out)
{
    *value_out = (uint32_t)rand();
    return true;
}


struct lrnd *const lrnd_stdlib_rand = &((struct lrnd){
    .max_value=RAND_MAX,        
    .next_value=stdlib_rand_next_value,
});


bool
lrnd_next_value(struct lrnd *lrnd, uint32_t *value_out)
{
    return lrnd->next_value(value_out);
}


A  => lrnd.h +22 -0
@@ 1,22 @@
#ifndef LRND_H_INCLUDED
#define LRND_H_INCLUDED

#include <stdbool.h>
#include <stdint.h>


struct lrnd {
    uint32_t const max_value;
    bool (*next_value)(uint32_t *value_out);
};


extern struct lrnd *const lrnd_stdlib_rand;


bool
lrnd_next_value(struct lrnd *lrnd, uint32_t *value_out);


#endif


A  => lrnd_tests.c +23 -0
@@ 1,23 @@
#include <assert.h>
#include <stdlib.h>
#include "lrnd.h"


static void
test_next_value_for_stdlib_rand(void)
{
    uint32_t value = UINT32_MAX;
    bool result = lrnd_next_value(lrnd_stdlib_rand, &value);
    assert(result);
    assert(value >= 0);
    assert(value <= RAND_MAX);
}


int
main(int argc, char *argv[])
{
    test_next_value_for_stdlib_rand();
    return EXIT_SUCCESS;
}