LinuxQuestions.org
Help answer threads with 0 replies.
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 03-27-2012, 02:30 AM   #1
JDC10
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Rep: Reputation: Disabled
Red face How to write a script to pick a random number 1-10 to generate a winner


I am very new to scripting and I have to write a script that a random number 1-10 is picked to be the winner by the script.

These are the requirements:
1. The game will only last 20 seconds
2. Does the script actually run
3. Does it give the users a countdown to the start of the game
4. Does it generate a random winning number
5. Does it calculate a start and end time
 
Old 03-27-2012, 03:34 AM   #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
So I am guessing this is for a class which is teaching you these things? Assuming the latter is the case, what have you tried and where are you stuck?
 
Old 03-27-2012, 09:08 AM   #3
JDC10
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Question I completed the sudo code

This is where I am and don't know where else to go..

#!/bin/bash
#set n to 10
n=0
#continue until $n equals 10
while [ $n -le 10 ]
do
echo $n
n=$(( n+1 ))
sleep 2
done

It runs but I do not know what else to do..HELP!!!!!
 
Old 03-27-2012, 09:56 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
Firstly, when posting code, please place it in [code][/code] tags to show formatting.

So you have created a loop that increases n from 0 to 10 and pauses between numbers for 2 seconds. Is this your timer?

What do you know about asking the user for input?

How do you create a once of random number?

It seems unclear if the script is going to generate a single random number that the user waits for or is the user trying to guess the number the script creates?

Also, just as you used (()) to increase 'n' by one, you should also use it for the while test, ie.
Code:
while (( n <= 10 ))
 
Old 03-27-2012, 04:04 PM   #5
JDC10
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks for you assistance.

No, this is not the timer. The 2 seconds pause is to slow down the count when the script is running.

I have heard about asking the user for input, but do not know much about it. I will research.

The script is suppose to generate a single random number to be the winning number.
I created the random number selection using the: "$RANDOM"

I have to set the timer for the game to be completed for 20 seconds. That will complete the first assignment.

The second assignment consist of these requirements:
Can it show the login of users in real time?
Does it accurately count users?
Does it save the data and show the winner at the end of the game?
 
Old 03-28-2012, 06:38 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I'd like to give some general advice, if I may.

The thing to understand about scripting is that, in essence, it works by executing individual commands, which can be either external or built into the shell. Scripts (generally) run these commands sequentially. You execute one command, process its output as necessary, then execute another. This is where flow control comes in; loops and conditionals allow you to control when and how the commands are executed, in particular allowing you to run the same commands over and over with varying input.

Grail has already given you the basic idea on how to proceed. Start by clearly defining your requirements. What exactly does your script need to do, step-by-step? Then break it down and figure out how to do each operation independently. What command(s) will give you the list of logged-in users, for example? How would you count them? How would you extract the information you want and output the results?

When you figure out the basic operations, you can then work on applying the sequence you want them to run in, and what data needs to be transferred between the commands (including anything retrieved from the user or other external sources). This usually involves wrapping them up in a structure of loops, conditionals, and variables to control how they execute, to create your final script.


Do note, however, that there are often several different ways to achieve a desired outcome, and you may find yourself modifying your ideas as you go along. Just keep at it and you'll get there eventually.


Here are a few useful bash scripting references:
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://www.linuxcommand.org/index.phphttp://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://wiki.bash-hackers.org/start
http://ss64.com/bash/


The first links in particular provide a very good overview of the basics, and common problems and errors. I also recommend learning how to use (and I mean really use) the most common scripting tools sed, awk, find, and regular expressions, ASAP. You'll find some overviews of these tools in the above links, or I can give you some more detailed ones if you're interested.
 
Old 03-28-2012, 11:19 AM   #7
JDC10
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Smile Thanks

Thank you David the H. The links you recommended is very helpful, but I would appreciate the links to the most common scripting tools and regular expressions. Since I just started learn, I need every help I can get.

Thanks again.
 
Old 03-28-2012, 11:44 AM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
In bash you can use the RANDOM variable to get random numbers. You can seed it first to get better random numbers, using whatever you want.

Code:
RANDOM=$RANDOM
echo $(( $RANDOM % 10 + 1 ))
A little tip there.
 
Old 03-28-2012, 02:03 PM   #9
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:
but I would appreciate the links to the most common scripting tools and regular expressions
"Common" has a lot of different meanings to each person, but what we are trying to tell you is that there are many tools that can do the job and it is up to you to pick which
you wish to use and how in depth you wish to learn each one. Ultimately all the links provided by David serve the purpose of getting you better acquainted with bash scripting.

So pick one, or a handful, and dive in
 
Old 03-28-2012, 04:59 PM   #10
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Quote:
Originally Posted by JDC10 View Post
I have to write a script that a random number 1-10 is picked to be the winner by the script.
This just describes a script choosing a number that is a winner; I think that you actually mean that, at some point, the script picks a number that is the 'correct answer' and that user(s) will be able to enter a guess and if the user(s) guess the same number that the script has picked, they will have won.

Is that interpretation correct, and/or in line with your interpretation?

And, you should be clear about 'number'. Could the script choose 3.141592654 and only if the user picked that exact same number do they get to win? I don't think that is what you mean, although 3.141592654 is a number between 1 and 10, so it does fit with the problem statement.

Quote:
Originally Posted by JDC10 View Post
I am very new to scripting and I have to write a script that a random number 1-10 is picked to be the winner by the script.

1. The game will only last 20 seconds
2. Does the script actually run
3. Does it give the users a countdown to the start of the game
4. Does it generate a random winning number
5. Does it calculate a start and end time
I think that 1 means that, from the start of the game, the user only has 20 seconds to enter a winning selection, and either
  • if a selection is entered after that it can't win
  • a selection cannot be entered after 20 seconds
From what i can see, you could do it either way, but you'd have to choose one and go with it.
For 2, presumably the answer has to be yes.
The implications of 3 seem to be
  • there should be a countdown at the start of the game, and it should be visible to the user(s)
  • the word 'users' seems to imply that there can be more than one user simultaneously; is this correct?
4 is a little difficult, in ways beyond the scope of this answer; practically all random numbers are slightly non-random in perhaps subtle ways; this can't be an issue in introductory scripting, can it. The only practical (useful?) note I can think of here is that if the random number comes from some system facility, it is likely to be random enough; if you come up with some way of generating a random number yourself, there is always a possibility that you do it wrongly. This should be a hint. Further, you are likely to get a random number; in the note about 3.14.... above, you will have probably thought 'That isn't what I want'.

if there is a conversion from what you get to what you want, be careful that you have taken care to make all of the numbers equally likely, if you want a fair game.

Note also that there does not seem to be a requirement to give a message to the user that they have won, although it could be argued that the script is fairly useless if the script knows whether the user wins, but doesn't let anyone know. On the other hand, the problem definition is the problem definition...

Quote:
Originally Posted by JDC10 View Post
I completed the sudo code
I'm sorry, but this is almost funny - an easy (and irrelevant) mistake: 'sudo' is a Unix command, and isn't what you want. It is a pun on the word (fragment) pseudo. (Try 'man sudo' for the meaning). What you want here, I believe, is 'pseudocode', which is rather different.


Quote:
Originally Posted by JDC10 View Post
#!/bin/bash
#set n to 10
n=0
What? When the comment suggests setting n to 10, why don't you do that? Well, actually the code may be what you intended and the comment might be totally the wrong thing. (OTOH, having #!/bin/bash as the first line is good.)


Quote:
Originally Posted by JDC10 View Post
#continue until $n equals 10
while [ $n -le 10 ]
do
echo $n
n=$(( n+1 ))
sleep 2
done
OK, you seem to just be writing an incrementing number to the screen successively. You might want to have a look at the man page for echo, just to see if there are any options that you might want to use.

You don't seem to be doing any choosing a winning number, getting any input from the user, comparing the user's input against the winning number or taken any action as a result. You do understand that, don't you?

Quote:
but I would appreciate the links to the most common scripting tools and regular expressions. Since I just started learn, I need every help I can get.
The most common scripting tool, by a long way, is a simple text editor (commonly vi/vim or emacs, although my choice would be joe - however the point is choose any simple text editor that you feel happy with - you don't need at complex tool like an IDE for writing programs in a scripting language); for something like this, you don't need regular expressions (something like 'man regex' will allow you to confirm this).
 
Old 03-28-2012, 05:37 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 03-29-2012, 06:55 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
salasi's post again demonstrates just how important it is to clearly define your requirements first.

I would however say that a general text editor is a tool needed for creating scripts, rather than a tool used in scripts*. So while it's certainly smart to choose a good one and learn how to use it effectively, it's not really necessary to know much about them in order to be a good scripter. It's quite rare to actually launch an editor from within a script, after all.

(*ed could be considered an exception here; and it's a program I intend to get around to learning some day. )

By "common" tools, I meant of course the ones that cover the most-commonly needed scripting functions, and conversely generate the most questions in places like this. To my mind, that mostly means grep, sed, awk, and find, along with the coreutils and the use of regular expressions. Not to mention the shell's built-in features.

Naturally this isn't an exhaustive list, and you can add others as well, e.g. wget and other network-related tools. But that's getting into specific application territory. The above tools will cover the vast majority of your basic scripting needs.


This is the list of links that I've compiled for the above programs (except for grep and the coreutils -- see below):

Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/

Here are a couple of links about using find:
http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html

A couple of regular expressions tutorials:
http://mywiki.wooledge.org/RegularExpression
http://www.grymoire.com/Unix/Regular.html


The grymoire links in particular are all highly recommended, but do note that they are written by a Unix guy, and there are generally some differences between the implementations of the programs he's talking about and the gnu tools that most Linux distros use. They're great for learning the basic common-denominator stuff though.

For the grep and the coreutils, simply call up info grep and info coreutils on your system and read away. The Advanced Bash Scripting Guide (link in my previous post), also has a good section on external tools that you can peruse.
 
  


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
[SOLVED] Generate Long Digits Random Number in C / C++ devUnix Programming 25 09-20-2011 01:20 PM
[SOLVED] I need to generate random number either 1 or 0? mvmacd Linux - General 4 01-21-2010 06:50 PM
Generate a random number from a bourne shell script lothario Linux - Software 2 03-01-2007 11:01 PM
Generate random number with C++ TruongAn Programming 5 11-09-2005 12:01 AM
Python problems trying to generate a random number davholla Programming 0 10-27-2003 04:07 AM

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

All times are GMT -5. The time now is 09:42 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