<-- Go Back

randomm.h (share it)

randomm.h is a C library to generate sequences of pseudo-random numbers by using a seed, which is a positive integer. It can be easly ported to other languages. Its algorithm is used by this online tool and this password generator.

randomm.h:

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

/* Copyright (C) 2019 Pasquale Frega */
/* All rights reserved */
/* http://pasqualefrega.antiblog.com/ */

/* Released under the simplified BSD license */

/* last edit: 13-Jun-2019 */

/* begin */

/* Maximum random number (try to change only with another odd 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 "randomm.h"

int main(void) {
 int _count;

 srand(20190613);

 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);
}


-

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

Copyright (c) 2019, 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.