From: Nicholas Nethercote Date: Fri, 8 Jul 2005 04:08:59 +0000 (+0000) Subject: Add a simple random number generator to m_libcbase so we don't have X-Git-Tag: svn/VALGRIND_3_0_0~188 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=91c18e835ee51b0a7fbd5932fd44ad9f0f40cb6e;p=thirdparty%2Fvalgrind.git Add a simple random number generator to m_libcbase so we don't have to use the one from glibc. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@4130 --- diff --git a/coregrind/m_libcbase.c b/coregrind/m_libcbase.c index 7369cb38ce..f8c4275edd 100644 --- a/coregrind/m_libcbase.c +++ b/coregrind/m_libcbase.c @@ -471,6 +471,21 @@ void VG_(ssort)( void* base, SizeT nmemb, SizeT size, #undef SORT } +static UInt seed = 0; + +void VG_(srandom)(UInt s) +{ + seed = s; +} + +// This random number generator is based on the one suggested in Kernighan +// and Ritchie's "The C Programming Language". +UInt VG_(random)(void) +{ + seed = (1103515245*seed + 12345); + return seed; +} + /*--------------------------------------------------------------------*/ /*--- end ---*/ /*--------------------------------------------------------------------*/ diff --git a/coregrind/m_skiplist.c b/coregrind/m_skiplist.c index 796b1d39d7..f9ca188068 100644 --- a/coregrind/m_skiplist.c +++ b/coregrind/m_skiplist.c @@ -93,8 +93,6 @@ #include "pub_core_mallocfree.h" #include "pub_core_skiplist.h" -#include - #define SKIPLIST_DEBUG 0 #define SK_MAXHEIGHT 20 /* 2^20 elements */ @@ -121,7 +119,7 @@ static inline Int get_height(void) { UInt ret = 0; - while((ret < SK_MAXHEIGHT - 1) && (random() & 1)) + while((ret < SK_MAXHEIGHT - 1) && (VG_(random)() & 1)) ret++; return ret; diff --git a/include/pub_tool_libcbase.h b/include/pub_tool_libcbase.h index fb59b39b93..d1f7f5c1d1 100644 --- a/include/pub_tool_libcbase.h +++ b/include/pub_tool_libcbase.h @@ -114,6 +114,11 @@ extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size, /* Returns the base-2 logarithm of x. */ extern Int VG_(log2) ( Int x ); +// A pseudo-random number generator returning a random UInt, and its +// seed function. +extern void VG_(srandom) ( UInt seed ); +extern UInt VG_(random) ( void ); + #endif // __PUB_TOOL_LIBCBASE_H /*--------------------------------------------------------------------*/