include support for a simple pseudo number generator
Quoted 9 lines · Andy Pont
Sascha Hauer wrote...
Review
Quoted 4 lines
(quoted diff) +4
-0+#define RAND_MAX 32767 + +/* return a pseudo-random integer in the range [0, RAND_MAX] */ +unsigned int rand(void);*Snip*
Quoted 9 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+unsigned int rand(void) +{ + random_seed = random_seed * 1103515245 + 12345; + return (random_seed / 65536) % 32768; +}The return value on this function seems wrong as it will always return a value between 0 and 32767 irrespective of what
RAND_MAXis set to which doesn't agree with the comment on the function prototype.
Well the way the rand function is implemented it is not suitable for RAND_MAX > 32767. Ok, changed it for the attached patch.
I originally thought there would be discussion about the pros and cons of random ethernet addresses, but about not this patch ;)
Sascha
commit dcd5248b7dceb918c5c5d9b045893421ddc6c112
Author: Sascha Hauer <[email protected]> Date: Fri Jun 11 14:10:36 2010 +0200
include support for a simple pseudo number generator
Signed-off-by Sascha Hauer <[email protected]>
include/net.h +1 -0
@@ -16,6 +16,7 @@#include <linux/types.h> #include <param.h> #include <malloc.h> +#include <stdlib.h> #include <asm/byteorder.h> /* for nton* / ntoh* stuff */
include/stdlib.h new file +16 -0
@@ -0,0 +1,16 @@ +#ifndef __STDLIB_H +#define __STDLIB_H + +#define RAND_MAX 32767 + +/* return a pseudo-random integer in the range [0, RAND_MAX] */ +unsigned int rand (void ); + +/* set the seed for rand () */ +void srand (unsigned int seed); + +/* fill a buffer with pseudo-random data */ +void get_random_bytes (char *buf,int len); + + +#endif /* __STDLIB_H */
lib/Makefile +1 -0
@@ -28,6 +28,7 @@ obj-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o obj-y += glob.o obj-y += notifier.o obj-y += copy_file.o +obj-y += random.o obj-y += lzo/ obj-$(CONFIG_LZO_DECOMPRESS) += decompress_unlzo.o obj-$(CONFIG_PROCESS_ESCAPE_SEQUENCE) += process_escape_sequence.o
lib/random.c new file +26 -0
@@ -0,0 +1,26 @@ +#include <common.h> +#include <stdlib.h> + +static unsigned int random_seed; + +#if RAND_MAX >32767 +#error this rand implementation is for RAND_MAX < 32678 only. +#endif + +unsigned int rand (void ) +{ + random_seed = random_seed *1103515245 +12345 ; +return (random_seed /65536 ) % (RAND_MAX +1 ); +} + +void srand (unsigned int seed) +{ + random_seed = seed; +} + +void get_random_bytes (char *buf,int len) +{ +while (len--) + *buf++ =rand () %256 ; +} +
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox