How to write a script to pick a random number 1-10 to generate a winner
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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?
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.
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.
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.
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.
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
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
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
#!/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
#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).
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):
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.