* random once but not twice
@ 2017-11-11 18:57 Emanuel Berg
2017-11-11 21:52 ` Mikael Magnusson
0 siblings, 1 reply; 12+ messages in thread
From: Emanuel Berg @ 2017-11-11 18:57 UTC (permalink / raw)
To: zsh-users
Just wrote this. Strange thing is, random-video
gives me randomized files, but not
play-random-video, which is just an interface -
or that the intention, at least :)
Can anyone figure out what is going on?
All other suggestions how to improve the code,
both style and function, is appreciated, as
always...
random-video () {
local vid_dir=${1:-.}
local ext=${2:-mkv}
local vid
local -a vids
local num_vids
local vid_num
vids=("${(@f)$(ls ${vid_dir}/*.${ext})}")
num_vids=${#vids}
vid_num=$(( RANDOM % $num_vids + 1 ))
vid=$vids[$vid_num]
echo $vid
}
alias rv=random-video
play-random-video () {
local vid_dir=${1:-/mnt-disk/mm/survivor/au/04}
local vid
vid=$(random-video $vid_dir)
echo "playing $vid"
# pl $vid # local function to omx player
}
alias prv=play-random-video
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: random once but not twice
2017-11-11 18:57 random once but not twice Emanuel Berg
@ 2017-11-11 21:52 ` Mikael Magnusson
2017-11-11 22:26 ` Emanuel Berg
0 siblings, 1 reply; 12+ messages in thread
From: Mikael Magnusson @ 2017-11-11 21:52 UTC (permalink / raw)
To: Zsh Users
On Sat, Nov 11, 2017 at 7:57 PM, Emanuel Berg <moasen@zoho.com> wrote:
> Just wrote this. Strange thing is, random-video
> gives me randomized files, but not
> play-random-video, which is just an interface -
> or that the intention, at least :)
>
> Can anyone figure out what is going on?
>
> All other suggestions how to improve the code,
> both style and function, is appreciated, as
> always...
>
> random-video () {
> local vid_dir=${1:-.}
> local ext=${2:-mkv}
>
> local vid
> local -a vids
> local num_vids
> local vid_num
>
> vids=("${(@f)$(ls ${vid_dir}/*.${ext})}")
> num_vids=${#vids}
>
> vid_num=$(( RANDOM % $num_vids + 1 ))
> vid=$vids[$vid_num]
>
> echo $vid
> }
> alias rv=random-video
>
> play-random-video () {
> local vid_dir=${1:-/mnt-disk/mm/survivor/au/04}
> local vid
> vid=$(random-video $vid_dir)
> echo "playing $vid"
> # pl $vid # local function to omx player
> }
> alias prv=play-random-video
The subshell in the second function ensures that the RANDOM state in
the parent shell stays the same.
--
Mikael Magnusson
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: random once but not twice
2017-11-11 21:52 ` Mikael Magnusson
@ 2017-11-11 22:26 ` Emanuel Berg
2017-11-11 22:32 ` Emanuel Berg
0 siblings, 1 reply; 12+ messages in thread
From: Emanuel Berg @ 2017-11-11 22:26 UTC (permalink / raw)
To: zsh-users
Mikael Magnusson wrote:
> The subshell in the second function ensures
> that the RANDOM state in the parent shell
> stays the same.
But why doesn't the next invocation produce
a new value? As in
random-test () {
repeat 10 echo $RANDOM
}
$ random-test
9235
18141
28176
10397
14564
21514
2335
31921
7989
8201
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: random once but not twice
2017-11-11 22:26 ` Emanuel Berg
@ 2017-11-11 22:32 ` Emanuel Berg
2017-11-11 22:46 ` Emanuel Berg
0 siblings, 1 reply; 12+ messages in thread
From: Emanuel Berg @ 2017-11-11 22:32 UTC (permalink / raw)
To: zsh-users
echo $(echo $RANDOM) # always the same
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: random once but not twice
2017-11-11 22:32 ` Emanuel Berg
@ 2017-11-11 22:46 ` Emanuel Berg
2017-11-12 19:08 ` Peter Stephenson
0 siblings, 1 reply; 12+ messages in thread
From: Emanuel Berg @ 2017-11-11 22:46 UTC (permalink / raw)
To: zsh-users
Emanuel Berg wrote:
> echo $(echo $RANDOM) # always the same
OK, I learned on #zsh@freenode that $RANDOM
doesn't do anything but is inherited from the
parent shell where there has been no access and
thus no new value.
: $RANDOM; vid=$(random-video $vid_dir)
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: random once but not twice
2017-11-11 22:46 ` Emanuel Berg
@ 2017-11-12 19:08 ` Peter Stephenson
2017-11-12 20:02 ` Emanuel Berg
2017-11-13 0:15 ` Emanuel Berg
0 siblings, 2 replies; 12+ messages in thread
From: Peter Stephenson @ 2017-11-12 19:08 UTC (permalink / raw)
To: zsh-users
On Sat, 11 Nov 2017 23:46:07 +0100
Emanuel Berg <moasen@zoho.com> wrote:
> Emanuel Berg wrote:
>
> > echo $(echo $RANDOM) # always the same
>
> OK, I learned on #zsh@freenode that $RANDOM
> doesn't do anything but is inherited from the
> parent shell where there has been no access and
> thus no new value.
>
> : $RANDOM; vid=$(random-video $vid_dir)
You can also work around with the following rand48 function.
(rand48; print $REPLY)
(rand48; print $REPLY)
pws
# Genrate a 48-bit pseudo-random number as a floating point value between
# 0 and 1 in $REPLY.
#
# This version is slow but works aroud the fact that the random number
# seed doesn't propagate back from a subshell by storing the seed in
# a file.
#
# You can speicify a file name, else ${ZDOTDIR:-~}/.zsh-rand48-seeed is used,
# but if using the default be careful about simultaneous accesses from
# multipe shells.
zmodload zsh/mathfunc
local file=${1:-${ZDOTDIR:-~}/.zsh-rand48-seed}
local seed
if [[ -f $file ]]; then
seed=$(<$file)
fi
typeset -g REPLY=$(( rand48(seed) ))
print $seed >$file
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: random once but not twice
2017-11-12 19:08 ` Peter Stephenson
@ 2017-11-12 20:02 ` Emanuel Berg
2017-11-12 20:15 ` Bart Schaefer
2017-11-13 0:15 ` Emanuel Berg
1 sibling, 1 reply; 12+ messages in thread
From: Emanuel Berg @ 2017-11-12 20:02 UTC (permalink / raw)
To: zsh-users
Peter Stephenson wrote:
> You can also work around with the following
> rand48 function.
>
> (rand48; print $REPLY)
> (rand48; print $REPLY)
$ (rand48; print $REPLY)
zsh: command not found: rand48
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: random once but not twice
2017-11-12 20:02 ` Emanuel Berg
@ 2017-11-12 20:15 ` Bart Schaefer
2017-11-12 20:18 ` Emanuel Berg
0 siblings, 1 reply; 12+ messages in thread
From: Bart Schaefer @ 2017-11-12 20:15 UTC (permalink / raw)
To: Zsh Users
On Sun, Nov 12, 2017 at 12:02 PM, Emanuel Berg <moasen@zoho.com> wrote:
> Peter Stephenson wrote:
>
>> You can also work around with the following
>> rand48 function.
>>
>
> zsh: command not found: rand48
The entire remainder of PWS's message, starting with "# Genrate a
48-bit pseudo-random number ...", was the implementation of a shell
script or function intended to be named 'rand48'.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: random once but not twice
2017-11-12 19:08 ` Peter Stephenson
2017-11-12 20:02 ` Emanuel Berg
@ 2017-11-13 0:15 ` Emanuel Berg
2017-11-13 4:56 ` Bart Schaefer
1 sibling, 1 reply; 12+ messages in thread
From: Emanuel Berg @ 2017-11-13 0:15 UTC (permalink / raw)
To: zsh-users
Peter Stephenson wrote:
> You can also work around with the following
> rand48 function.
>
> (rand48; print $REPLY) (rand48; print $REPLY)
Works! Only I don't understand this syntax.
What does it do?
seed=$(<$file)
and
print $seed >$file
I suppose is some intentional circular
short-circuit to generate gibberish data?
BTW you could benefit from a spellchecker :)
Here is a spell-checked version of your comment
# Generate a 48-bit pseudo-random number as
# a floating point value between 0 and 1 in
# $REPLY.
#
# This version is slow but works around the fact
# that the random number seed doesn't propagate
# back from a subshell by storing the seed in
# a file.
#
# You can specify a file name, else
# ${ZDOTDIR:-~}/.zsh-rand48-seed is used, but
# if using the default be careful about
# simultaneous accesses from multiple shells.
Thanks :)
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: random once but not twice
2017-11-13 0:15 ` Emanuel Berg
@ 2017-11-13 4:56 ` Bart Schaefer
2017-11-13 5:07 ` Emanuel Berg
0 siblings, 1 reply; 12+ messages in thread
From: Bart Schaefer @ 2017-11-13 4:56 UTC (permalink / raw)
To: Zsh Users
On Sun, Nov 12, 2017 at 4:15 PM, Emanuel Berg <moasen@zoho.com> wrote:
>
> Works! Only I don't understand this syntax.
> What does it do?
>
> seed=$(<$file)
RTM:
A command enclosed in parentheses preceded by a dollar sign, like
`$(...)', or quoted with grave accents, like ``...`', is replaced with
its standard output, with any trailing newlines deleted. If the
substitution is not enclosed in double quotes, the output is broken
into words using the IFS parameter. The substitution `$(cat FOO)' may
be replaced by the equivalent but faster `$(<FOO)'. In either case,
if the option GLOB_SUBST is set, the output is eligible for filename
generation.
> and
>
> print $seed >$file
>
> I suppose is some intentional circular
> short-circuit to generate gibberish data?
The comment in the function explains this:
# This version is slow but works around the fact
# that the random number seed doesn't propagate
# back from a subshell by storing the seed in
# a file.
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2017-11-13 5:07 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-11 18:57 random once but not twice Emanuel Berg
2017-11-11 21:52 ` Mikael Magnusson
2017-11-11 22:26 ` Emanuel Berg
2017-11-11 22:32 ` Emanuel Berg
2017-11-11 22:46 ` Emanuel Berg
2017-11-12 19:08 ` Peter Stephenson
2017-11-12 20:02 ` Emanuel Berg
2017-11-12 20:15 ` Bart Schaefer
2017-11-12 20:18 ` Emanuel Berg
2017-11-13 0:15 ` Emanuel Berg
2017-11-13 4:56 ` Bart Schaefer
2017-11-13 5:07 ` Emanuel Berg
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).