LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-05-2014, 11:44 AM   #1
alsaf
Member
 
Registered: Mar 2009
Distribution: Lubuntu 13.10
Posts: 40

Rep: Reputation: 0
UK Euromillions Lottery number generator written in Bash script


This is my first fairly complex Bash script that I have written so thought I'd post it just in case somebody is looking for a program that does the functionality in the title:

Code:
#!/bin/bash
#===================================================================================
#
#   FILE: lottery.sh 
#
#   USAGE: ./lottery.sh
#
# DESCRIPTION: Generate 5 random numbers between 1 and 50 and two random
#              lucky numbers between 1 and 11 to be used in the UK Euromillions lottery. 
#              The 5 normal numbers are picked using the following format to give an even 
#              spread:
#              * 1 number between 1 to 50
#              * 2 numbers between 1 and 25
#              * 2 numbers between 26 to 50   
#
#      OPTIONS: --- 
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: --- 
#      COMPANY: ---
#      VERSION: --- 
#      CREATED: ---  
#     REVISION: --- 
#===================================================================================

#=== FUNCTION ================================================================
#        NAME: check_number
# DESCRIPTION: checks number passed to it that it doesn't exist in array 
# PARAMETER 1: Boolean variable to say if it exists or not
# PARAMETER 2: Number to be checked in array
#===============================================================================
check_number() {
  local  __check_number=$1
  local lottery_number=$2
  local x

  for ((x = 0; x < ${#lottery[@]} ; x ++ ))  
  do 
    if [ ${lottery[$x]} -eq $lottery_number ]
    then
      eval $__check_number=true
    fi
  done
}

#=== FUNCTION ================================================================
#        NAME: generate_main_number
# DESCRIPTION: Generate main lottery numbers, ensure there are no duplicates 
#              and put in array
# PARAMETER 1: lottery number that is to be passed back 
# PARAMETER 2: Type of number which determines which range it will be in ie. 
#              either between 1 and 25 or between 26 and 50 
#===============================================================================
generate_main_number() {
  local number

  number_type=$1

  number_picked=false
  until $number_picked ; do 
    if [ $number_type == "First" ]
    then    
      number=$(( $RANDOM % 24 + 1 ))
    else
      number=$(( $RANDOM % 24 + 26 ))
    fi
    number_exists=false
    check_number number_exists $number
    if ! $number_exists
    then
      return $number 
      number_picked=true
    fi 
  done
}

#=== FUNCTION ================================================================
#        NAME: main 
# DESCRIPTION: Main function to drive program 
# PARAMETER 1: ---
#===============================================================================
main () {
  local x
  # Generate first number
  lottery[0]=$(( $RANDOM % 49 +1 ))

  # Generate second and third lottery main number betwen one and fifty
  for ((x = 0; x <= 1 ; x ++ ))  
  do
    generate_main_number "First"
    lottery[1+$x]=$?
  done

  # Generate forth and fifth lottery main number between one and fifty
  for ((x = 0; x <= 1 ; x ++ ))  
  do
    generate_main_number "Second"
    lottery[3+$x]=$?
 done

  # Generate 2 lucky numbers ensuring they are two different numbers
  lottery[6]=$(( $RANDOM % 10 +1 )) 
  correct_number=false
  until  $correct_number ; do 
    lottery[7]=$(( $RANDOM % 10 +1 )) 
    if [ ${lottery[6]} -ne ${lottery[7]} ] 
    then
      correct_number=true
    fi
  done

  # display lottery number
  echo ${lottery[@]}
}

# Calls main function to start program
main

Last edited by alsaf; 05-06-2014 at 01:53 AM.
 
Old 05-05-2014, 01:09 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I do not see any reason for the use of eval nor do I understand why there had to be 2 separate loops made to call the same function?

Also, what is the purpose of creating the 'main' function when all you then do is simply call it anyway, ie. why not just have the data in the main function left as the actual
code of the script as the function itself never needs to be called again so would seem redundant.

Your description also states:
Quote:
Generate 5 random numbers between 1 and 50 and two random
Yet you later state:
Quote:
# * 1 number between 1 to 50
# * 2 numbers between 1 and 25
# * 2 numbers between 26 to 50
Which would imply that they are not random between 1 and 50 as your solution will never arrive at the numbers 1,2,3,4,5 which is just as random as any other numbers within the associated boundary.

Lastly, you supply code that even according to you has a bug in it:
Quote:
@bug There is something wrong with this function
Which at least to me meant straight off that I would not run this code to try it. Admittedly better than hiding the fact, but I would suggest fixing said bug before raising this type of ticket.
 
Old 05-06-2014, 02:03 AM   #3
alsaf
Member
 
Registered: Mar 2009
Distribution: Lubuntu 13.10
Posts: 40

Original Poster
Rep: Reputation: 0
Thanks for the feedback grail.

I am returning a variable back to calling function and found using google that eval was the way to do it with string/boolean variables.

I use the main function because it is a habit from programming with other languages.

In my comments, I mentioned that I was using 5 random numbers between 1 and 50 but to get these numbers I use two ranges to get a better spread of numbers. This will given two numbers in the range 1 to 25, 2 numbers in the range 26 and 50 and 1 between 1 and 50. If I had did a total random pick of 5 numbers of 1 between 50, there is a possibility that it could generate numbers 1,2,3,4 and 5 or numbers that are not so evenly spread which as anybody who does the lottery knows doesn't happen. Hope that makes sense?

As with bug comment, I had a problem with that function as I didn't realise in Bash that you need to declare local variables so as I had used the x variable twice in different functions, it was getting treated as a global variable. It is now working and I have removed the comment from code I had supplied in original post.
 
Old 05-06-2014, 02:35 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
numbers that are not so evenly spread which as anybody who does the lottery knows doesn't happen
Well I know my local lottery hasn't had all numbers in sequence, but we have had 4 out of the 6 and whilst unlikely, all combinations have the same chance

As for returning a value, why not simply return 1 or 0 seeing you are only looking at true or false. I will admit that eval is not a real problem in this context, but by the same token it is also not required.

Nice work though
 
Old 05-06-2014, 02:58 PM   #5
alsaf
Member
 
Registered: Mar 2009
Distribution: Lubuntu 13.10
Posts: 40

Original Poster
Rep: Reputation: 0
My previous reply was a quick one before I went to work this morning which is why it doesn't make much sense grammar wise, never mind what I was trying to get at!

The inspiration for this script was because I had previously used 'lucky dip' numbers which were ones that were picked randomly by the shops lottery ticket machine. I was beginning to notice a trend of these luck dip numbers having a narrow spread where for example 2 or 3 of the normal numbers where between 1 and 10 which, while they could be part of a weeks winning numbers, it is more statistically improbable for me to win than with one with a more even spread. My script, which has a more even spread, in theory should give me a better chance than with these lucky dip numbers. The thing is my script can in theory produce 3 numbers in a narrow spread and I was thinking as a further improvement to have a function that checks to see if two numbers are in a narrow spread and if so, it will be changed or left depending on a random generated number is either 0 or 1. I don't think for the slightest that this script or the proposed change I am going to make my chances of winning any greater but it is fun and exciting to think that the possibility of the most random numbers possible could be the winning numbers.

That is a good idea for returning the boolean variable. It will add a wee bit more extra code to the script but will make it more readable just incase I need to amend the script in future and I forget the reason whey the eval was needed!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Oscillating number script written in Thyme? steviebob Programming 13 04-03-2010 01:51 PM
programming a cpu load generator with bash script petee Linux - Newbie 4 09-16-2009 08:24 PM
bash script - progressive world list generator musther Programming 2 01-10-2008 04:22 PM
Need help with my backup script written in bash! guy12345 Programming 14 05-25-2007 05:09 AM
written a password generator in C, get it here lepricaun General 1 08-09-2004 07:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration