zsh-workers
 help / color / mirror / code / Atom feed
* $RANDOM in function in pipe do not get reseeded
@ 2017-03-30 11:25 tharvik
  2017-03-30 13:25 ` Eric Cook
  0 siblings, 1 reply; 2+ messages in thread
From: tharvik @ 2017-03-30 11:25 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 406 bytes --]

Hello,

There is some strange behavior with $RANDOM, because you don't
seed after forking.

For a testcase, here you go
--
Define a function echoing the value of $RANDOM
```
get_random() {
        echo "${RANDOM}"
}
```
Run it trough a pipe
```
get_random | cat
```
If you run it multiple times, the output is the same.
--

There is the patch linked, which fixes it.

Have a nice day!
-- 
Valérian Rousset

[-- Attachment #2: 0001-reseed-srand-when-forking.patch --]
[-- Type: text/x-diff, Size: 700 bytes --]

>From 9d9430cb430dd5a7ceaf498fbdfe197c0964e5eb Mon Sep 17 00:00:00 2001
From: tharvik <tharvik@users.noreply.github.com>
Date: Thu, 30 Mar 2017 11:55:58 +0200
Subject: [PATCH] reseed srand when forking

---
 Src/exec.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/Src/exec.c b/Src/exec.c
index 137130e..7f77252 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -312,9 +312,11 @@ zfork(struct timeval *tv)
 	return -1;
     }
 #ifdef HAVE_GETRLIMIT
-    if (!pid)
+    if (!pid) {
 	/* set resource limits for the child process */
 	setlimits(NULL);
+	srand((unsigned int)(tv->tv_sec + tv->tv_usec)); /* newly seed RANDOM in child */
+    }
 #endif
     return pid;
 }
-- 
2.10.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: $RANDOM in function in pipe do not get reseeded
  2017-03-30 11:25 $RANDOM in function in pipe do not get reseeded tharvik
@ 2017-03-30 13:25 ` Eric Cook
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Cook @ 2017-03-30 13:25 UTC (permalink / raw)
  To: zsh-workers

On 03/30/2017 07:25 AM, tharvik@gmail.com wrote:
> Hello,
> 
> There is some strange behavior with $RANDOM, because you don't
> seed after forking.
> 
> For a testcase, here you go
> --
> Define a function echoing the value of $RANDOM
> ```
> get_random() {
>         echo "${RANDOM}"
> }
> ```
> Run it trough a pipe
> ```
> get_random | cat
> ```
> If you run it multiple times, the output is the same.
> --
> 
> There is the patch linked, which fixes it.
> 
> Have a nice day!
> 

from zshparam:
RANDOM <S>
       A pseudo-random integer from 0 to 32767, newly generated each time this parameter is referenced.  The  random  number
       generator can be seeded by assigning a numeric value to RANDOM.

       The  values  of  RANDOM form an intentionally-repeatable pseudo-random sequence; subshells that reference RANDOM will
       result in identical pseudo-random values unless the value of RANDOM is referenced or seeded in the  parent  shell  in
       between subshell invocations.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-03-30 13:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-30 11:25 $RANDOM in function in pipe do not get reseeded tharvik
2017-03-30 13:25 ` Eric Cook

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).