ProductsLogo
LogoStudy Documents
LogoAI Grader
LogoAI Answer
LogoAI Code Checker
LogoPlagiarism Checker
LogoAI Paraphraser
LogoAI Quiz
LogoAI Detector
PricingBlogAbout Us
logo

Implementation of a Random Number Generator

Verified

Added on  2020/07/22

|13
|2779
|228
AI Summary
This document presents a past paper solution on Bash shell scripting, offering insights into the language's features compared to others. The solution includes a script that generates random numbers between a minimum and maximum value divisible by a specific number. The script is designed to be portable and compatible with various UNIX versions. It also contains a loop that tests the results of the random number generator, ensuring it produces accurate outcomes.

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
Bash scripts

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
Introduction
Party is the shell, or order language interpreter, for the gnu operating
system. Title is an acronym for the ‘Bourne-Again Shell’, a juga on Stephen
Bourne, the writer of the direct ancestor from the current UNIX shell you will
need, which appeared in the 7th Edition Bell Labs Analysis version of UNIX.
Party is largely compatible with sh plus incorporates useful features through
the Korn shell ksh as well as the C shell csh. It really is intended to be a
conformant execution of the IEEE posix Cover and Tools portion of the
particular IEEE posix specification (IEEE Standard 1003. 1). It provides
functional improvements over you will need for both interactive plus
programming use. While the gnu operating system offers other shells,
including an edition of csh, Bash could be the default shell. Like some other
gnu software, Bash is pretty portable. It currently operates on nearly every
version associated with UNIX and a few other systems − independently-
supported ports can be found for ms-dos, os/2, plus Windows platforms.
A UNIX shell is both the command interpreter and a development language.
As a command interpreter, the particular shell provides the user interface
towards the rich set of gnu resources. These types of utilities be allowed by
programming language features to become combined. Files that contains
commands can be created, and turn into commands themselves. These
brand new commands have the same standing as system commands within
directories such as /bin, permitting users or groups to determine custom
environments to handle their common tasks. Covers may non-interactively
be used interactively or. In interactive setting, they accept input tapped out
from the keyboard. When carrying out non-interactively, shells execute
instructions read from a file. The shell allows execution associated with gnu
commands, each synchronously and asynchronously. The particular shell
waits for synchronous commands to complete before agreeing to more input;
asynchronous commands continue to execute within parallel with the shell
Document Page
although it executes and reads extra commands. The particular redirection
constructs permit fine-grained control of the output and insight of those
commands.
Basic Cover Features
Bash is an acronym with regard to ‘Bourne-Again Shell’. The particular
Bourne shell is the conventional UNIX shell originally authored by Stephen
Bourne. All the Bourne shell built-in instructions are available in Bash, The
rules regarding evaluation and quoting are usually taken from the posix
standards for the ‘standard’ UNIX cover.
Shell Syntax
When the covering reads input, it profits through a sequence of procedures.
When the beginning is indicated with the input of a comment, the shell
ignores the comment symbol (‘#’), as well as the rest of that relative
collection.
Quoting
Quoting is used to remove the unique meaning of certain figures or words to
the cover. Quoting can be used to disable special therapy for special
characters, to avoid reserved words from getting recognized as such, and to
avoid parameter expansion.
Escape Personality
A non-quoted backslash ‘\’ is the Bash escape personality. It preserves the
literal value of the next character that will follows, with the exception of
newline. In case a \newline pair appears, as well as the backslash itself is not
cited, the particular \newline is treated as being a relative line continuation
Single Quotes
Document Page
Attaching characters in single rates (‘’’) preserves the literal value of each
character inside the quotes. Just one quote might not occur in between
single quotes, whenever preceded by a backslash actually.
Dual Quotes
Enclosing characters within double quotes (‘"’) maintains the literal value of
most characters within the quotes, except for ‘$’, ‘‘’, ‘\’, plus, when history
expansion can be enabled, ‘! ’. Once the shell is in posix setting.
Comments In a no interactive covering, or an interactive cover in which the
interactive comments option to the particular shop built-in is allowed, the
word beginning with ‘#’ leads to that word and all leftover characters on that
collection to be ignored. An online shell without the interactive comments
choice enabled does not allow responses.
Shell Functions
Cover functions are a real method to group commands for later delivery
using a single name for your group. These are executed just like a "regular"
order. Once the true name of a cover function is used as an easy command
name, the list of instructions associated with that function title is executed.
Shell features are executed in the current covering context; no new
procedure is created to interpret all of them.
Shell Parameters
A variable is an entity that shops values. It's rather a true name, several, or
even one of the special characters the following. The variable is a parameter
denoted by a name. The value is had with a variable and zero or even more
attributes.

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Installing bash
Basic instructions for setting up Bash on the various backed platforms. The
particular distribution supports the gnu operating systems, virtually every
version of UNIX, and many non-Unix systems such as Interix and BeOS. Other
independent ports can be found for ms-dos, os/2, plus Windows platforms.
These are set up instructions for Bash.
The easiest way to compile Bash will be:
o Compact disc to the directory containing the original source code and
type ‘. /configure’ in order to configure Bash for your program. If you’re
using csh with an old version of Program V, you may have to type
‘sh. /configure’ rather to prevent csh from looking to execute configure
itself. Operating configure takes some correct time. While running, it
images messages telling which functions it is checking for.
o Type ‘make’ to put together Bash and build the particular bash bug
reporting screenplay.
o Optionally, type ‘make tests’ to run the Party test suite.
o Kind ‘make install’ to install party and bash bug. This can install the
manual web pages and Info file furthermore.
Bash script for range and it also displays eight
random amount between the ranges
#!/bin/bash
# random-between.sh
# Random number between two specified values.
Document Page
# Script by Bill Gradwohl, with minor modifications by the document author.
# Corrections in lines 187 and 189 by Anthony Le Clezio.
# Used with permission.
randomBetween() {
# Generates a positive or negative random number
#+ between $min and $max
#+ and divisible by $divisibleBy.
# Gives a "reasonably random" distribution of return values.
#
# Bill Gradwohl - Oct 1, 2003
syntax() {
# Function embedded within function.
echo
echo "Syntax: randomBetween [min] [max] [multiple]"
echo
echo -n "Expects up to 3 passed parameters, "
echo "but all are completely optional."
echo "min is the minimum value"
echo "max is the maximum value"
echo -n "multiple specifies that the answer must be "
echo "a multiple of this value."
echo " i.e. answer must be evenly divisible by this number."
echo
echo "If any value is missing, defaults area supplied as: 0 32767 1"
echo -n "Successful completion returns 0, "
echo "unsuccessful completion returns"
echo "function syntax and 1."
echo -n "The answer is returned in the global variable "
Document Page
echo "randomBetweenAnswer"
echo -n "Negative values for any passed parameter are "
echo "handled correctly."
}
local min=${1:-0}
local max=${2:-32767}
local divisibleBy=${3:-1}
# Default values assigned, in case parameters not passed to function.
local x
local spread
# Let's make sure the divisibleBy value is positive.
[ ${divisibleBy} -lt 0 ] && divisibleBy=$((0-divisibleBy))
# Sanity check.
if [ $# -gt 3 -o ${divisibleBy} -eq 0 -o ${min} -eq ${max} ]; then
syntax
return 1
fi
# See if the min and max are reversed.
if [ ${min} -gt ${max} ]; then
# Swap them.
x=${min}
min=${max}
max=${x}
fi
# If min is itself not evenly divisible by $divisibleBy,

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
#+ then fix the min to be within range.
if [ $((min/divisibleBy*divisibleBy)) -ne ${min} ]; then
if [ ${min} -lt 0 ]; then
min=$((min/divisibleBy*divisibleBy))
else
min=$((((min/divisibleBy)+1)*divisibleBy))
fi
fi
# If max is itself not evenly divisible by $divisibleBy,
#+ then fix the max to be within range.
if [ $((max/divisibleBy*divisibleBy)) -ne ${max} ]; then
if [ ${max} -lt 0 ]; then
max=$((((max/divisibleBy)-1)*divisibleBy))
else
max=$((max/divisibleBy*divisibleBy))
fi
fi
# ---------------------------------------------------------------------
# Now, to do the real work.
# Note that to get a proper distribution for the end points,
#+ the range of random values has to be allowed to go between
#+ 0 and abs(max-min)+divisibleBy, not just abs(max-min)+1.
# The slight increase will produce the proper distribution for the
#+ end points.
# Changing the formula to use abs(max-min)+1 will still produce
#+ correct answers, but the randomness of those answers is faulty in
Document Page
#+ that the number of times the end points ($min and $max) are returned
#+ is considerably lower than when the correct formula is used.
# ---------------------------------------------------------------------
spread=$((max-min))
# Omair Eshkenazi points out that this test is unnecessary,
#+ since max and min have already been switched around.
[ ${spread} -lt 0 ] && spread=$((0-spread))
let spread+=divisibleBy
randomBetweenAnswer=$(((RANDOM%spread)/divisibleBy*divisibleBy+mi
n))
return 0
# However, Paulo Marcel Coelho Aragao points out that
#+ when $max and $min are not divisible by $divisibleBy,
#+ the formula fails.
#
# He suggests instead the following formula:
# rnumber = $(((RANDOM%(max-min+1)+min)/divisibleBy*divisibleBy))
}
# Let's test the function.
min=-14
max=20
divisibleBy=3
# Generate an array of expected answers and check to make sure we get
#+ at least one of each answer if we loop long enough.
Document Page
declare -a answer
minimum=${min}
maximum=${max}
if [ $((minimum/divisibleBy*divisibleBy)) -ne ${minimum} ]; then
if [ ${minimum} -lt 0 ]; then
minimum=$((minimum/divisibleBy*divisibleBy))
else
minimum=$((((minimum/divisibleBy)+1)*divisibleBy))
fi
fi
# If max is itself not evenly divisible by $divisibleBy,
#+ then fix the max to be within range.
if [ $((maximum/divisibleBy*divisibleBy)) -ne ${maximum} ]; then
if [ ${maximum} -lt 0 ]; then
maximum=$((((maximum/divisibleBy)-1)*divisibleBy))
else
maximum=$((maximum/divisibleBy*divisibleBy))
fi
fi
# We need to generate only positive array subscripts,
#+ so we need a displacement that that will guarantee
#+ positive results.
disp=$((0-minimum))
for ((i=${minimum}; i<=${maximum}; i+=divisibleBy)); do

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
answer[i+disp]=0
done
# Now loop a large number of times to see what we get.
loopIt=1000 # The script author suggests 100000,
#+ but that takes a good long while.
for ((i=0; i<${loopIt}; ++i)); do
# Note that we are specifying min and max in reversed order here to
#+ make the function correct for this case.
randomBetween ${max} ${min} ${divisibleBy}
# Report an error if an answer is unexpected.
[ ${randomBetweenAnswer} -lt ${min} -o ${randomBetweenAnswer} -gt
${max} ] \
&& echo MIN or MAX error - ${randomBetweenAnswer}!
[ $((randomBetweenAnswer%${divisibleBy})) -ne 0 ] \
&& echo DIVISIBLE BY error - ${randomBetweenAnswer}!
# Store the answer away statistically.
answer[randomBetweenAnswer+disp]=$
((answer[randomBetweenAnswer+disp]+1))
done
# Let's check the results
for ((i=${minimum}; i<=${maximum}; i+=divisibleBy)); do
[ ${answer[i+disp]} -eq 0 ] \
Document Page
&& echo "We never got an answer of $i." \
|| echo "${i} occurred ${answer[i+disp]} times."
done
exit 0
Conclusion
An understanding has been presented by this particular paper of bash,
compared its features along with those of other shells, plus hinted at
features within the next release, bash-2. 0. Party is a solid replacement for
you will need. It really is sufficiently portable to run upon nearly every
version of UNIX from 4. 3 BSD in order to SVR4. 2, and several UNIX work
likes, plus robust enough to replace you will need on most of those systems,
it is very near to POSIX. 2-conformant in POSIX mode, and is getting quicker.
It is not, unfortunately, getting smaller, yet there are many optional features.
It is extremely easy to build a small subset to use as a direct replacement for
/bin/sh. Bash has thousands of customers worldwide, all of whom possess
helped to make it better. An additional testament to the benefits of free
software program.
Document Page
References
[1] S. R. Bourne, ‘‘UNIX Time-Sharing System: The UNIX Shell’’, Bell System
Technical Journal, 57(6), July-August, 1978, pp. 1971-1990.
[2] Morris Bolsky and David Korn, The KornShell Command and Programming
Language, Prentice Hall, 1989.
[3] Bill Joy, An Introduction to the C Shell, UNIX User’s Supplementary
Documents, University of California at Berkeley, 1986.
[4] IEEE, IEEE Standard for Information Technology -- Portable Operating
System Interface (POSIX) Part 2: Shell and Utilities, 1992.
1 out of 13
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]