最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

random number genarator function

工作和技术 crifan 1595浏览 0评论

// random.cpp : generate the random number
//随机数产生原理参考:【转】线同余数产生随机数算法

/* http://hi.baidu.com/serial_story/blog/item/2ae42295b88ef40d7af480ba.html */

#include "stdafx.h"
#include "stdio.h"
#include "time.h"

/***************** should belong to random.h *****************/
#ifndef _RANDOM_H__
#define _RANDOM_H__

#ifndef sint8
typedef signed   char sint8;
#endif

#ifndef uint8
typedef unsigned char uint8;
#endif

#ifndef sint16
typedef signed   short sint16;
#endif

#ifndef uint16
typedef unsigned short uint16;
#endif

#ifndef sint32
typedef signed   int   sint32;
#endif

#ifndef uint32
typedef unsigned int   uint32;
#endif

#ifndef sint64
typedef signed   long sint64;
#endif

#ifndef uint64
typedef unsigned long uint64;
#endif

#endif /* _RANDOM_H__ */
/***************** should belong to random.h *****************/

/***************** should belong to random.c *****************/

/*
*      $File: random.c $
*      $Revision: 0.1 $
*      LANGUAGE: ANSI C
*/

/*! file
*
* author Crifan Li
*
* brief Genarate the random number.
*
* Genarate the random number.
*/

/*
******************************************************************************
* INCLUDES
******************************************************************************
*/
//#include "random.h"

/*
******************************************************************************
* DEFINES
******************************************************************************
*/

/*
******************************************************************************
* LOCAL FUNCTION PROTOTYPES
******************************************************************************
*/

/*
******************************************************************************
* LOCAL VARIABLES
******************************************************************************
*/
static uint32 randomNr = 1;

/*
******************************************************************************
* GLOBAL VARIABLE DEFINITIONS
******************************************************************************
*/

/*
******************************************************************************
* LOCAL FUNCTIONS
******************************************************************************
*/

/*
******************************************************************************
* GLOBAL FUNCTIONS
******************************************************************************
*/

/*!
*****************************************************************************
* brief Use Mixed same remainder method to generate the pseudo random number.
*
* Use Mixed same remainder method to generate the pseudo random number.
*
* brief description of Linear Same Remainder Method to generate random number:
* Xi = (Xi-1 * A + C ) mod M
* Xi is the output random number, Xi-1 is the last time generated random number
* when C=0, called Linear Same Remainder Method;
* when C!=0, called Mixed Same Remainder Method;
* even the c!=0 has some advantage, but not so much outstanding, so, generally, we use c=0.
* but for avoid the former is 0, and the next is always 0 except set new seed, so here we use c=1
* moro details please refer : http://hi.baidu.com/serial_story/blog/item/2ae42295b88ef40d7af480ba.html
*
* param maxRange – max number of the output random, max allowable one is 2^31=2147483648
*
* return random number: 0 ~ maxRange
*****************************************************************************
*/
uint32 getRandom(uint32 maxRange)
{
const uint32 a = 1103515245 ; /* a is multiplier, useful ones:   1103515245, 1220703125,32719,16807 */
const uint32 c = 1 ;    /* a is Increment */
const uint32 m = 2147483648 ; /* a is mod, useful ones: 2^31=2147483648, 2^15=32768 */
//static randomNr = 1;

randomNr = (randomNr*a+c)%m;

/* then use "human figure hash / tent hash " method */
/* details in : http://www.yuanma.org/data/2007/0802/article_2788.htm */
if (randomNr < (m/2))
   randomNr = 2*randomNr;
else
   randomNr = 2*(m – randomNr) + 1;

return randomNr%(maxRange+1);
}

/*!
*****************************************************************************
* brief Set the new seed of the random function to get different random number with the different seed.
*
* Set the new seed of the random function to get different random number with the different seed.
* a good example of set this new seed with the system time
*
* param newSeed – new seed of the random number
*
* return none
*****************************************************************************
*/
void setSeed(uint32 newSeed)
{
randomNr = newSeed;
}
/***************** should belong to random.c *****************/

/***************** should belong to random_test.c *****************/
//#include "stdafx.h"
//#include "stdio.h"
//#include "time.h"
//#include "random.h"
int main(int argc, char* argv[])
{
time_t currTime;
uint32 range;
uint8 i;

currTime = time(NULL);
printf("The current time is %d.n", currTime);

range = 16; /* just want to get 0-16*/
//setSeed(currTime);
setSeed(currTime%range);

for (i =0; i < 20; i++)
{
   //printf("The %d time , the random number is %d.n", i, getRandom(range));
   printf("%dn", getRandom(range));
}

return 0;
}
/***************** should belong to random_test.c *****************/

转载请注明:在路上 » random number genarator function

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
79 queries in 0.195 seconds, using 22.08MB memory