<-- Go to Home Page

random.h

random.h is a C library to generate sequences of pseudo-random numbers by using a seed, which is a positive integer. It could be easly ported to other languages, too.

random.h:

/* random.h - a C library to generate pseudo-random numbers */

/* Copyright (C) 2018 Pasquale Frega */
/* All rights reserved */

/* Released under the semplified BSD license */

/* last edit: 17-Feb-2018 */

/* begin */

/* Maximum random number */

#define RAND_MAX 32767

unsigned int SEED = 1;
unsigned int TIME = 1;

/* Change SEED value */

void srand(unsigned int seed) {
 SEED = seed;
 TIME = 1;
}

/* Return a pseudo-random number */

int rand(void) {
 SEED += TIME;
 if(SEED != RAND_MAX) {
  while(SEED < RAND_MAX)
   SEED *= SEED + 1;
  while(SEED >= RAND_MAX)
   SEED -= RAND_MAX;
 }
 TIME++;
 return((int)SEED);
}

/* end */


test.c:

#include <stdio.h>
#include "random.h"

int main(void) {
 int _count;

 srand(20180217);

 printf("10 numbers from 0 to 32767:\n");
 for(_count = 0; _count <= 9; _count++)
  printf("%d\n", rand());

 /* Using % operator changes the range and increases random */

 printf("10 numbers from 0 to 32767:\n");
 for(_count = 0; _count <= 9; _count++)
  printf("%d\n", (rand() % 32768));

 printf("10 numbers from 0 to 10:\n");
 for(_count = 0; _count <= 9; _count++)
  printf("%d\n", (rand() % 11));

 printf("10 numbers from 5 to 10:\n");
 for(_count = 0; _count <= 9; _count++)
  printf("%d\n", ((rand() % 6) + 5));

 printf("10 numbers from -5 to 5:\n");
 for(_count = 0; _count <= 9; _count++)
  printf("%d\n", ((rand() % 11) - 5));

 printf("10 numbers from 0.0 to 1.0:\n");
 for(_count = 0; _count <= 9; _count++)
  printf("%.1f\n", ((rand() % 11) * 0.1));

 return(0);
}


-

random.h (and every thing with it) is released under the semplified BSD license:

Copyright (c) 2018, Pasquale Frega
All rights reserved.

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

1. Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.
2. 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 OWNER 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.