You are not logged in.

#1 2018-08-10 17:10:42

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,744

seen

seen script.

What is supposed to do: Limit number of repetitions from a random system, examples:

a. With one argument: string (limit and database are defaults)

while true; do seen $((RANDOM % 21 + 1)) ; done
# will run slowly, since it uses default limit of 20 members.
# and will generate exact pattern in this case where ( members - limit = 1 ) = bad.

Warning: If 'limit >= possibleMembers', we have a lock = bad. Probably recommended limit is around 1/3 of possibleMembers.

b. With three arguments: limit, database, string

# Don't show me the same cookie for at least 200 times
while true; do seen 200 fortunesdb "$(fortune -s)" ; done
# or better
while true; do seen 200 fortunesdb "$(fortune -s)" && {echo ; sleep 1 ;} done

c. In some script

until var="$(seen 200 fortunesdb "$(fortune -s)" 2>/dev/null)"; do : ; done ; echo "$var"

c2. In some script with some locking prevention

#!/bin/bash

# var
lm="300"
db="fortunesdb"

# action
c="0"
unlock="20" # This could depend on limit/members ratio
until 

    var="$(seen $lm $db "$(fortune -s)" 2>/dev/null)"
    
do 

    # lock prevention
    (( c++ ))
    if (( c == unlock )); then 
         >&2 echo "lock prevented"
        break
    fi
    
done  

# report
if ! [[ -z "$var" ]]; then

    echo "$c $var"

else

    echo " ##### no fortune for you ##### "

fi

d. And perhaps a really basic example

seen ohnonot ; echo $?
ohnonot
0

seen bronto ; echo $? 
bronto
0
 
seen ohnonot ; echo $? 
1
 
seen bronto ; echo $? 
1

e. Bruteforce/blackbox measurement (Checking for an approximate number of possibleMembers)

for i in {1..10000}; do fortune -s ; done | grep -ve "--" | sort | uniq | wc -l
1036

so I'd take something like 300 for a limit.

f. edit: And a little distribution test with a 'seen' system of 100 members with limit set to 50 vs random choice and hacked to get around 500 results:

seen https://paste.debian.net/plain/1037522
rnd  https://paste.debian.net/plain/1037523

Actually random (RND) does pretty well there, repeating member only twice 3 times, however first repeat is already in 25th place. Looking at the first line, RND repeats it on 29th place, while SEEN repeats it on line 57. Now looking at the RND distribution more closely, you can spot silly stuff like:

23 █████████████████████
71 ██████████████████████████████████████████████████████████████████
77 ████████████████████████████████████████████████████████████████████████
71 ██████████████████████████████████████████████████████████████████
54 ██████████████████████████████████████████████████

or really ugly moments like

56 ████████████████████████████████████████████████████
78 █████████████████████████████████████████████████████████████████████████
56 ████████████████████████████████████████████████████
80 ███████████████████████████████████████████████████████████████████████████
23 █████████████████████
12 ███████████
55 ███████████████████████████████████████████████████
55 ███████████████████████████████████████████████████

Last edited by brontosaurusrex (2018-08-13 10:58:37)

Offline

#2 2018-08-10 17:32:30

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

Re: seen

took me a while to understand what this is about...

from testing my improved bl-fortune script i must say yes, it would be best if there was a way to

  1. randomize fortune output, but

  2. in a way that they don't repeat until all have been seen

the usual way to accomplish this is to make a randomised list, and then just go through that list until it's used up.

Offline

#3 2018-08-10 17:37:52

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,744

Re: seen

^I thought about that, but I think there will be possible repetitive behavior on the edges, so a moving window is better imho (FILO = First In, Last Out). Also that would assume that one is in full control of that specific random system. But i will stand corrected if proven wrong.

That would be randomize in the beginning example i guess (edge case thought)?

echo "1\n2\n3" | shuf
1
2
3

echo "1\n2\n3" | shuf
3
1
2

See how 3 and 3 are could be repeating.

When with seen you get

while true; do seen 1 num $((RANDOM % 3 + 1)) ; done
3
2
3
2
3
2
1
2
3
2
3
1
2
1
2
3
2
1
3

Last edited by brontosaurusrex (2018-08-10 21:40:46)

Offline

Board footer

Powered by FluxBB