Browse Source

Add one to internal result of random number generator to avoid the value zero, from Freddie Chopin

git-svn-id: http://svn.code.sf.net/p/nuttx/code/trunk@5404 42af7a65-404d-4744-a932-0658087f49c3
sbg
patacongo 12 years ago
parent
commit
b0b339c242
  1. 2
      nuttx/ChangeLog
  2. 2
      nuttx/configs/sim/README.txt
  3. 173
      nuttx/libc/stdlib/lib_rand.c

2
nuttx/ChangeLog

@ -3722,3 +3722,5 @@
* include/nuttx/input/keypad.h, arch/arm/src/calypso/calypso_keypad.c, and * include/nuttx/input/keypad.h, arch/arm/src/calypso/calypso_keypad.c, and
configs/compal_e99/nsh_highram: First cut at a standard keypad interface configs/compal_e99/nsh_highram: First cut at a standard keypad interface
definition. Contributed by Denis Carikli. definition. Contributed by Denis Carikli.
* libc/stdlib/lib_rand.c: Always add one to result congruential generators
to avoid the value zero. Suggested by Freddie Chopin.

2
nuttx/configs/sim/README.txt

@ -389,7 +389,7 @@ nx11
NOTES: NOTES:
1. If you do not have the call to sim_tcinitializE(0), the build 1. If you do not have the call to sim_tcinitialize(0), the build
will mysteriously fail claiming that is can't find up_tcenter() will mysteriously fail claiming that is can't find up_tcenter()
and up_tcleave(). That is a consequence of the crazy way that and up_tcleave(). That is a consequence of the crazy way that
the simulation is built and can only be eliminated by calling the simulation is built and can only be eliminated by calling

173
nuttx/libc/stdlib/lib_rand.c

@ -1,4 +1,4 @@
/************************************************************ /****************************************************************************
* libc/stdlib/lib_rand.c * libc/stdlib/lib_rand.c
* *
* Copyright (C) 2007, 2011 Gregory Nutt. All rights reserved. * Copyright (C) 2007, 2011 Gregory Nutt. All rights reserved.
@ -31,25 +31,21 @@
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
* *
************************************************************/ ****************************************************************************/
/************************************************************ /****************************************************************************
* Compilation Switches
************************************************************/
/************************************************************
* Included Files * Included Files
************************************************************/ ****************************************************************************/
#include <sys/types.h> #include <sys/types.h>
#include <stdlib.h> #include <stdlib.h>
/************************************************************ /****************************************************************************
* Definitions * Pre-processor Definitions
************************************************************/ ****************************************************************************/
#ifndef CONFIG_LIB_RAND_ORDER #ifndef CONFIG_LIB_RAND_ORDER
#define CONFIG_LIB_RAND_ORDER 1 # define CONFIG_LIB_RAND_ORDER 1
#endif #endif
/* Values needed by the random number generator */ /* Values needed by the random number generator */
@ -65,20 +61,16 @@
#define RND3_CONSTP 997783 #define RND3_CONSTP 997783
#if CONFIG_LIB_RAND_ORDER == 1 #if CONFIG_LIB_RAND_ORDER == 1
# define RND_CONSTP RND1_CONSTP # define RND_CONSTP RND1_CONSTP
#elif CONFIG_LIB_RAND_ORDER == 2 #elif CONFIG_LIB_RAND_ORDER == 2
# define RND_CONSTP RND2_CONSTP # define RND_CONSTP RND2_CONSTP
#else #else
# define RND_CONSTP RND3_CONSTP # define RND_CONSTP RND3_CONSTP
#endif #endif
/************************************************************ /****************************************************************************
* Private Type Declarations
************************************************************/
/************************************************************
* Private Function Prototypes * Private Function Prototypes
************************************************************/ ****************************************************************************/
static unsigned int nrand(unsigned int nLimit); static unsigned int nrand(unsigned int nLimit);
static double_t frand1(void); static double_t frand1(void);
@ -89,132 +81,133 @@ static double_t frand3(void);
#endif #endif
#endif #endif
/********************************************************** /****************************************************************************
* Global Constant Data * Private Data
**********************************************************/ ****************************************************************************/
/************************************************************
* Global Variables
************************************************************/
/**********************************************************
* Private Constant Data
**********************************************************/
/************************************************************
* Private Variables
************************************************************/
static unsigned long g_nRandInt1; static unsigned long g_randint1;
#if (CONFIG_LIB_RAND_ORDER > 1) #if (CONFIG_LIB_RAND_ORDER > 1)
static unsigned long g_nRandInt2; static unsigned long g_randint2;
#if (CONFIG_LIB_RAND_ORDER > 2) #if (CONFIG_LIB_RAND_ORDER > 2)
static unsigned long g_nRandInt3; static unsigned long g_randint3;
#endif #endif
#endif #endif
/************************************************************ /****************************************************************************
* Private Functions * Private Functions
************************************************************/ ****************************************************************************/
static unsigned int nrand(unsigned int nLimit) static unsigned int nrand(unsigned int nLimit)
{ {
unsigned long nResult; unsigned long result;
double_t fRatio; double_t ratio;
/* Loop to be sure a legal random number is generated */ /* Loop to be sure a legal random number is generated */
do {
/* Get a random integer in the requested range */ do
{
/* Get a random integer in the requested range */
#if (CONFIG_LIB_RAND_ORDER == 1) #if (CONFIG_LIB_RAND_ORDER == 1)
fRatio = frand1(); ratio = frand1();
#elif (CONFIG_LIB_RAND_ORDER == 2) #elif (CONFIG_LIB_RAND_ORDER == 2)
fRatio = frand2(); ratio = frand2();
#else #else
fRatio = frand3(); ratio = frand3();
#endif #endif
/* Then, produce the return-able value */ /* Then, produce the return-able value */
nResult = (unsigned long)(((double_t)nLimit) * fRatio);
} while (nResult >= (unsigned long)nLimit); result = (unsigned long)(((double_t)nLimit) * ratio);
}
while (result >= (unsigned long)nLimit);
return (unsigned int)nResult; return (unsigned int)result;
}
} /* end nrand */
static double_t frand1(void) static double_t frand1(void)
{ {
unsigned long nRandInt; unsigned long randint;
/* First order congruential generator. One is added to the result of the
* generated value to avoid the value zero which breaks the logic.
*/
/* First order congruential generator */ randint = (RND1_CONSTK * g_randint1) % RND1_CONSTP + 1;
nRandInt = (RND1_CONSTK * g_nRandInt1) % RND1_CONSTP; g_randint1 = randint;
g_nRandInt1 = nRandInt;
/* Construct an floating point value in the range from 0.0 up to 1.0 */ /* Construct an floating point value in the range from 0.0 up to 1.0 */
return ((double_t)nRandInt) / ((double_t)RND_CONSTP);
} /* end frand */ return ((double_t)randint) / ((double_t)RND_CONSTP);
}
#if (CONFIG_LIB_RAND_ORDER > 1) #if (CONFIG_LIB_RAND_ORDER > 1)
static double_t frand2(void) static double_t frand2(void)
{ {
unsigned long nRandInt; unsigned long randint;
/* Second order congruential generator */ /* Second order congruential generator. One is added to the result of the
nRandInt = (RND2_CONSTK1 * g_nRandInt1 + RND2_CONSTK2 * g_nRandInt2) % * generated value to avoid the value zero which breaks the logic.
RND2_CONSTP; */
g_nRandInt2 = g_nRandInt1;
g_nRandInt1 = nRandInt; randint = (RND2_CONSTK1 * g_randint1 +
RND2_CONSTK2 * g_randint2) % RND2_CONSTP + 1;
g_randint2 = g_randint1;
g_randint1 = randint;
/* Construct an floating point value in the range from 0.0 up to 1.0 */ /* Construct an floating point value in the range from 0.0 up to 1.0 */
return ((double_t)nRandInt) / ((double_t)RND_CONSTP);
} /* end frand */ return ((double_t)randint) / ((double_t)RND_CONSTP);
}
#if (CONFIG_LIB_RAND_ORDER > 2) #if (CONFIG_LIB_RAND_ORDER > 2)
static double_t frand3(void) static double_t frand3(void)
{ {
unsigned long nRandInt; unsigned long randint;
/* Third order congruential generator. One is added to the result of the
* generated value to avoid the value zero which breaks the logic.
*/
randint = (RND3_CONSTK1 * g_randint1 +
RND3_CONSTK2 * g_randint2 +
RND3_CONSTK2 * g_randint3) % RND3_CONSTP + 1;
/* Third order congruential generator */ g_randint3 = g_randint2;
nRandInt = (RND3_CONSTK1 * g_nRandInt1 + RND3_CONSTK2 * g_nRandInt2 + g_randint2 = g_randint1;
RND3_CONSTK2 * g_nRandInt3) % RND3_CONSTP; g_randint1 = randint;
g_nRandInt3 = g_nRandInt2;
g_nRandInt2 = g_nRandInt1;
g_nRandInt1 = nRandInt;
/* Construct an floating point value in the range from 0.0 up to 1.0 */ /* Construct an floating point value in the range from 0.0 up to 1.0 */
return ((double_t)nRandInt) / ((double_t)RND_CONSTP);
} /* end frand */ return ((double_t)randint) / ((double_t)RND_CONSTP);
}
#endif #endif
#endif #endif
/************************************************************ /****************************************************************************
* Public Functions * Public Functions
************************************************************/ ****************************************************************************/
/************************************************************
/****************************************************************************
* Function: srand, rand * Function: srand, rand
************************************************************/ ****************************************************************************/
void srand(unsigned int seed) void srand(unsigned int seed)
{ {
g_nRandInt1 = seed; g_randint1 = seed;
#if (CONFIG_LIB_RAND_ORDER > 1) #if (CONFIG_LIB_RAND_ORDER > 1)
g_nRandInt2 = seed; g_randint2 = seed;
(void)frand1(); (void)frand1();
#if (CONFIG_LIB_RAND_ORDER > 2) #if (CONFIG_LIB_RAND_ORDER > 2)
g_nRandInt3 = seed; g_randint3 = seed;
(void)frand2(); (void)frand2();
#endif #endif
#endif #endif
}
} /* end srand */
int rand(void) int rand(void)
{ {
return (int)nrand(32768); return (int)nrand(32768);
}
} /* end rand */

Loading…
Cancel
Save