Rewrote the random_rand() pseudo random number generator to use the libc rand() function instead

This commit is contained in:
adamdunkels 2009-02-11 11:09:59 +00:00
parent 585620c102
commit 13a3029435
2 changed files with 11 additions and 30 deletions

View File

@ -28,14 +28,16 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* @(#)$Id: rand.h,v 1.4 2007/09/04 08:48:54 bg- Exp $ * @(#)$Id: rand.h,v 1.5 2009/02/11 11:09:59 adamdunkels Exp $
*/ */
/* -*- C -*- */ /* -*- C -*- */
/* @(#)$Id: rand.h,v 1.4 2007/09/04 08:48:54 bg- Exp $ */ /* @(#)$Id: rand.h,v 1.5 2009/02/11 11:09:59 adamdunkels Exp $ */
#ifndef RAND_H #ifndef RAND_H
#define RAND_H #define RAND_H
#include "sys/cc.h"
#undef RAND_MAX #undef RAND_MAX
#define RAND_MAX 0x7fff #define RAND_MAX 0x7fff

View File

@ -28,45 +28,24 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* @(#)$Id: random.c,v 1.2 2008/02/10 12:30:57 oliverschmidt Exp $ * @(#)$Id: random.c,v 1.3 2009/02/11 11:09:59 adamdunkels Exp $
*/ */
/*
From:
D. Seetharam and S. Rhee, ``An Efficient Random Number Generator
for Low-Power Sensor Networks'' First IEEE Workshop on Embedded
Networked Sensors (EmNets-I), November 2004.
http://www.dseetharam.org/papers/emnets.pdf
*/
#include "lib/random.h" #include "lib/random.h"
#include "lib/rand.h"
#include "sys/clock.h" #include "sys/clock.h"
static clock_time_t time; /*---------------------------------------------------------------------------*/
static unsigned short key;
void void
random_init(unsigned short seed) random_init(unsigned short seed)
{ {
key = seed; srand(seed);
} }
/*---------------------------------------------------------------------------*/
unsigned short unsigned short
random_rand(void) random_rand(void)
{ {
unsigned short rv, tv; return (unsigned short)rand();
rv = tv = 0;
tv = (unsigned short)(time + clock_time());
rv = tv ^ key;
key = ~tv + 4711;
tv = ~rv;
time = tv;
return rv;
} }
/*---------------------------------------------------------------------------*/