aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKarl Heuer1995-01-19 23:37:10 +0000
committerKarl Heuer1995-01-19 23:37:10 +0000
commit4bb8c8b7921a2aaa46359e136fa6058b1ff3bad9 (patch)
tree5c0f85eff635868a7a63bb8aed18cf7ab1eb42da /src
parent99175c23d34107a2d4d7b1eb00d40b4c0d6704a3 (diff)
downloademacs-4bb8c8b7921a2aaa46359e136fa6058b1ff3bad9.tar.gz
emacs-4bb8c8b7921a2aaa46359e136fa6058b1ff3bad9.zip
(seed_random): Renamed from srandom.
(get_random): Renamed from random. Return VALBITS random bits.
Diffstat (limited to 'src')
-rw-r--r--src/sysdep.c92
1 files changed, 66 insertions, 26 deletions
diff --git a/src/sysdep.c b/src/sysdep.c
index fe26e1f3879..e4ef51e519f 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -2699,38 +2699,79 @@ bcmp (b1, b2, length) /* This could be a macro! */
2699#endif /* not BSTRING */ 2699#endif /* not BSTRING */
2700 2700
2701#ifndef HAVE_RANDOM 2701#ifndef HAVE_RANDOM
2702#ifndef random 2702#ifdef random
2703#define HAVE_RANDOM
2704#endif
2705#endif
2706
2707/* Figure out how many bits the system's random number generator uses.
2708 `random' and `lrand48' are assumed to return 31 usable bits.
2709 BSD `rand' returns a 31 bit value but the low order bits are unusable;
2710 so we'll shift it and treat it like the 15-bit USG `rand'. */
2711
2712#ifndef RAND_BITS
2713# ifdef HAVE_RANDOM
2714# define RAND_BITS 31
2715# else /* !HAVE_RANDOM */
2716# ifdef HAVE_LRAND48
2717# define RAND_BITS 31
2718# define random lrand48
2719# else /* !HAVE_LRAND48 */
2720# define RAND_BITS 15
2721# if RAND_MAX == 32767
2722# define random rand
2723# else /* RAND_MAX != 32767 */
2724# if RAND_MAX == 2147483647
2725# define random() (rand () >> 16)
2726# else /* RAND_MAX != 2147483647 */
2727# ifdef USG
2728# define random rand
2729# else
2730# define random() (rand () >> 16)
2731# endif /* !BSD */
2732# endif /* RAND_MAX != 2147483647 */
2733# endif /* RAND_MAX != 32767 */
2734# endif /* !HAVE_LRAND48 */
2735# endif /* !HAVE_RANDOM */
2736#endif /* !RAND_BITS */
2703 2737
2704long 2738void
2705random () 2739seed_random (arg)
2740 long arg;
2706{ 2741{
2707#ifdef HAVE_LRAND48 2742#ifdef HAVE_RANDOM
2708 return lrand48 (); 2743 srandom ((unsigned int)arg);
2709#else
2710/* The BSD rand returns numbers in the range of 0 to 2e31 - 1,
2711 with unusable least significant bits. The USG rand returns
2712 numbers in the range of 0 to 2e15 - 1, all usable. Let us
2713 build a usable 30 bit number from either. */
2714#ifdef USG
2715 return (rand () << 15) + rand ();
2716#else 2744#else
2717 return (rand () & 0x3fff8000) + (rand () >> 16); 2745# ifdef HAVE_LRAND48
2718#endif
2719#endif
2720}
2721
2722srandom (arg)
2723 int arg;
2724{
2725#ifdef HAVE_LRAND48
2726 srand48 (arg); 2746 srand48 (arg);
2727#else 2747# else
2728 srand (arg); 2748 srand ((unsigned int)arg);
2749# endif
2729#endif 2750#endif
2730} 2751}
2731 2752
2732#endif /* no random */ 2753/*
2733#endif /* not HAVE_RANDOM */ 2754 * Build a full Emacs-sized word out of whatever we've got.
2755 * This suffices even for a 64-bit architecture with a 15-bit rand.
2756 */
2757long
2758get_random ()
2759{
2760 long val = random ();
2761#if VALBITS > RAND_BITS
2762 val = (val << RAND_BITS) ^ random ();
2763#if VALBITS > 2*RAND_BITS
2764 val = (val << RAND_BITS) ^ random ();
2765#if VALBITS > 3*RAND_BITS
2766 val = (val << RAND_BITS) ^ random ();
2767#if VALBITS > 4*RAND_BITS
2768 val = (val << RAND_BITS) ^ random ();
2769#endif /* need at least 5 */
2770#endif /* need at least 4 */
2771#endif /* need at least 3 */
2772#endif /* need at least 2 */
2773 return val & ((1L << VALBITS) - 1);
2774}
2734 2775
2735#ifdef WRONG_NAME_INSQUE 2776#ifdef WRONG_NAME_INSQUE
2736 2777
@@ -4977,4 +5018,3 @@ dlclose ()
4977} 5018}
4978 5019
4979#endif /* USE_DL_STUBS */ 5020#endif /* USE_DL_STUBS */
4980