Reindent server/core/random_jkiss.c
This commit is contained in:
parent
72dd159f98
commit
233f50f3e6
@ -18,15 +18,15 @@
|
||||
|
||||
/**
|
||||
* @file random_jkiss.c - Random number generator for the MariaDB Corporation MaxScale
|
||||
*
|
||||
*
|
||||
* See http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf for discussion of random
|
||||
* number generators (RNGs).
|
||||
*
|
||||
* @verbatim
|
||||
* Revision History
|
||||
*
|
||||
* Date Who Description
|
||||
* 26/08/15 Martin Brampton Initial implementation
|
||||
* Date Who Description
|
||||
* 26/08/15 Martin Brampton Initial implementation
|
||||
*
|
||||
* @endverbatim
|
||||
*/
|
||||
@ -53,18 +53,18 @@ static unsigned int random_jkiss_devrand(void);
|
||||
static void random_init_jkiss(void);
|
||||
|
||||
/***
|
||||
*
|
||||
*
|
||||
* Return a pseudo-random number that satisfies major tests for random sequences
|
||||
*
|
||||
*
|
||||
* @return uint Random number
|
||||
*
|
||||
*
|
||||
*/
|
||||
unsigned int
|
||||
unsigned int
|
||||
random_jkiss(void)
|
||||
{
|
||||
unsigned long long t;
|
||||
unsigned int result;
|
||||
|
||||
|
||||
spinlock_acquire(&random_jkiss_spinlock);
|
||||
if (!init)
|
||||
{
|
||||
@ -88,18 +88,23 @@ random_jkiss(void)
|
||||
/* Own code adapted from http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf */
|
||||
|
||||
/***
|
||||
*
|
||||
*
|
||||
* Obtain a seed random number from /dev/urandom if available.
|
||||
*
|
||||
*
|
||||
* @return uint Random number
|
||||
*
|
||||
*
|
||||
*/
|
||||
static unsigned int
|
||||
static unsigned int
|
||||
random_jkiss_devrand(void)
|
||||
{
|
||||
int fn;
|
||||
unsigned int r;
|
||||
if ((fn = open("/dev/urandom", O_RDONLY)) == -1) return 0;
|
||||
|
||||
if ((fn = open("/dev/urandom", O_RDONLY)) == -1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (read(fn, &r, sizeof(r)) != sizeof(r))
|
||||
{
|
||||
r = 0;
|
||||
@ -109,23 +114,41 @@ random_jkiss_devrand(void)
|
||||
}
|
||||
|
||||
/***
|
||||
*
|
||||
*
|
||||
* Initialise the generator using /dev/urandom if available, and warm up
|
||||
* with 1000 iterations
|
||||
*
|
||||
*
|
||||
*/
|
||||
static void
|
||||
random_init_jkiss(void)
|
||||
{
|
||||
int newrand, i;
|
||||
|
||||
spinlock_acquire(&random_jkiss_spinlock);
|
||||
if ((newrand = random_jkiss_devrand()) != 0) x = newrand;
|
||||
if ((newrand = random_jkiss_devrand()) != 0) y = newrand;
|
||||
if ((newrand = random_jkiss_devrand()) != 0) z = newrand;
|
||||
if ((newrand = random_jkiss_devrand()) != 0)
|
||||
if ((newrand = random_jkiss_devrand()) != 0)
|
||||
{
|
||||
x = newrand;
|
||||
}
|
||||
|
||||
if ((newrand = random_jkiss_devrand()) != 0)
|
||||
{
|
||||
y = newrand;
|
||||
}
|
||||
|
||||
if ((newrand = random_jkiss_devrand()) != 0)
|
||||
{
|
||||
z = newrand;
|
||||
}
|
||||
|
||||
if ((newrand = random_jkiss_devrand()) != 0)
|
||||
{
|
||||
c = newrand % 698769068 + 1; /* Should be less than 698769069 */
|
||||
}
|
||||
spinlock_release(&random_jkiss_spinlock);
|
||||
|
||||
|
||||
/* "Warm up" our random number generator */
|
||||
for (i = 0; i < 100; i++) random_jkiss();
|
||||
for (i = 0; i < 100; i++)
|
||||
{
|
||||
random_jkiss();
|
||||
}
|
||||
}
|
||||
|
@ -1,22 +1,38 @@
|
||||
/*
|
||||
#ifndef RANDOM_JKISS_H
|
||||
#define RANDOM_JKISS_H
|
||||
/*
|
||||
* This file is distributed as part of the MariaDB Corporation MaxScale. It is free
|
||||
* software: you can redistribute it and/or modify it under the terms of the
|
||||
* GNU General Public License as published by the Free Software Foundation,
|
||||
* version 2.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program; if not, write to the Free Software Foundation, Inc., 51
|
||||
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Copyright MariaDB Corporation Ab 2013-2014
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: random_jkiss.h
|
||||
* Author: mbrampton
|
||||
*
|
||||
* Created on 26 August 2015, 15:34
|
||||
*/
|
||||
|
||||
#ifndef RANDOM_JKISS_H
|
||||
#define RANDOM_JKISS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern unsigned int random_jkiss(void);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* RANDOM_H */
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user