summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embedvar.h1
-rw-r--r--intrpvar.h8
-rw-r--r--perl.c2
-rw-r--r--pp_sort.c2
-rw-r--r--util.c4
-rw-r--r--util.h6
6 files changed, 19 insertions, 4 deletions
diff --git a/embedvar.h b/embedvar.h
index 8b9842f9cc..76efb9104b 100644
--- a/embedvar.h
+++ b/embedvar.h
@@ -174,6 +174,7 @@
#define PL_incgv (vTHX->Iincgv)
#define PL_initav (vTHX->Iinitav)
#define PL_inplace (vTHX->Iinplace)
+#define PL_internal_random_state (vTHX->Iinternal_random_state)
#define PL_isarev (vTHX->Iisarev)
#define PL_known_layers (vTHX->Iknown_layers)
#define PL_langinfo_buf (vTHX->Ilanginfo_buf)
diff --git a/intrpvar.h b/intrpvar.h
index b6b20bcad9..766e552aed 100644
--- a/intrpvar.h
+++ b/intrpvar.h
@@ -830,6 +830,14 @@ PERLVAR(I, random_state, PL_RANDOM_STATE_TYPE)
PERLVARI(I, dump_re_max_len, STRLEN, 0)
+/* For internal uses of randomness, this ensures the sequence of
+ * random numbers returned by rand() isn't modified by perl's internal
+ * use of randomness.
+ * This is important if the user has called srand() with a seed.
+ */
+
+PERLVAR(I, internal_random_state, PL_RANDOM_STATE_TYPE)
+
/* If you are adding a U8 or U16, check to see if there are 'Space' comments
* above on where there are gaps which currently will be structure padding. */
diff --git a/perl.c b/perl.c
index a3f8ac367d..8046013992 100644
--- a/perl.c
+++ b/perl.c
@@ -261,6 +261,8 @@ perl_construct(pTHXx)
init_constants();
+ Perl_drand48_init_r(&PL_internal_random_state, seed());
+
SvREADONLY_on(&PL_sv_placeholder);
SvREFCNT(&PL_sv_placeholder) = SvREFCNT_IMMORTAL;
diff --git a/pp_sort.c b/pp_sort.c
index 5f39dba40e..604950910a 100644
--- a/pp_sort.c
+++ b/pp_sort.c
@@ -788,7 +788,7 @@ S_qsortsvu(pTHX_ SV ** array, size_t num_elts, SVCOMPARE_t compare)
size_t n;
SV ** const q = array;
for (n = num_elts; n > 1; ) {
- const size_t j = (size_t)(n-- * Drand01());
+ const size_t j = (size_t)(n-- * Perl_internal_drand48());
temp = q[j];
q[j] = q[n];
q[n] = temp;
diff --git a/util.c b/util.c
index b470681bdd..136e4ca1ec 100644
--- a/util.c
+++ b/util.c
@@ -4677,10 +4677,8 @@ Perl_get_hash_seed(pTHX_ unsigned char * const seed_buffer)
else
#endif /* NO_PERL_HASH_ENV */
{
- (void)seedDrand01((Rand_seed_t)seed());
-
for( i = 0; i < PERL_HASH_SEED_BYTES; i++ ) {
- seed_buffer[i] = (unsigned char)(Drand01() * (U8_MAX+1));
+ seed_buffer[i] = (unsigned char)(Perl_internal_drand48() * (U8_MAX+1));
}
}
#ifdef USE_PERL_PERTURB_KEYS
diff --git a/util.h b/util.h
index 12a1c470ee..4589808ee1 100644
--- a/util.h
+++ b/util.h
@@ -89,6 +89,12 @@ typedef struct PERL_DRAND48_T perl_drand48_t;
#define Perl_drand48_init(seed) (Perl_drand48_init_r(&PL_random_state, (seed)))
#define Perl_drand48() (Perl_drand48_r(&PL_random_state))
+#ifdef PERL_CORE
+/* uses a different source of randomness to avoid interfering with the results
+ * of rand() */
+#define Perl_internal_drand48() (Perl_drand48_r(&PL_internal_random_state))
+#endif
+
#ifdef USE_C_BACKTRACE
typedef struct {