Monday, August 18, 2014

Lottery support.

Play the lottery?  Here is a simple random number generator to pontificate the numbers.  Run it as many times as there are picks.


lg.sh:
echo Lottery generator
echo
echo -n "Enter number of balls: "
read nodf
echo -n "Enter number of choices: "
read b
declare -i X=$b
for i in $(seq 1 1 $nodf)
do
NUM=$[ ( $RANDOM % $X ) + 1 ];
echo "The winner is for ball number $i:" $NUM
done


$ ./lg.sh
Lottery generator
Enter number of balls: 5
Enter number of choices: 50
The winner is for ball number 1: 32
The winner is for ball number 2: 43
The winner is for ball number 3: 23
The winner is for ball number 4: 18
The winner is for ball number 5: 38

Don’t forget to “chmod +x” it. Great for choosing winners randomly in a contest. Write the number down after each toss. I will let you mod the code to save the numbers.

Note: it does not check for duplicates.

Another way to do it.

usage ./rndom choicesperball numberofballs

$ ./rndom.sh 50 5

Choices per ball are 50 and the number of balls is 5

Ball number 1 is 35
Ball number 2 is 31
Ball number 3 is 6
Ball number 4 is 42
Ball number 5 is 3

$ cat rndom.sh

#!/bin/bash
clear
echo "Choices per ball are $1 and the number of balls is $2"
echo
for (( c=1; c<=$2; c++ ))
do
    echo "Ball number $c is $[($RANDOM % $1)]"
done

Note: this is a good way to give out prizes at a meeting. Just make sure everyone has their own number.

One last variation:

$ shuf -i 1-49 -n18 | xargs -n6
41 34 28 4 36 45
7 2 31 25 38 14
44 32 6 17 11 46
$ shuf -i 1-49 -n24 | xargs -n6
41 13 10 45 29 30
47 43 33 9 32 34
18 36 14 44 48 8
38 31 26 6 35 39

$ shuf -i 1-49 -n28 | xargs -n7
34 10 5 45 43 35 2
42 18 7 22 30 47 23
13 6 19 49 48 4 11
24 8 20 29 31 17 27

No comments:

Post a Comment