LinuxQuestions.org
Help answer threads with 0 replies.
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-15-2011, 01:09 AM   #1
lupusarcanus
Senior Member
 
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Rep: Reputation: 146Reputation: 146
Question BASH Script and $RANDOM


Inspired by this thread, I wrote a small BASH script just for the hell of it.

Anyway, take a look at it first:
Code:
#! /bin/bash

C="0"
while (true)
do
A="$RANDOM"
B="$RANDOM"
let C++
if [ $A -eq $B ]; then
echo "Incredible! The cosmos have bestowed $A upon you as your lucky number."
echo "Your UNIX system selected the same number twice consecutively from the boundless pool of '/dev/random'. This is a highly improbable feat; you are truly blessed!"
echo
echo
echo "…of course, it took $C attempts to do it. :P"
break
fi
done
Two questions:
  • Is it possible for the script to ever break the loop?
  • Is there anything wrong (syntactically and/or logically) with the script?
Thanks for all help in advance; it is much appreciated even if the script is just for giggles.

Last edited by lupusarcanus; 03-15-2011 at 01:12 AM. Reason: English errors fixed.
 
Old 03-15-2011, 03:21 AM   #2
corp769
Guru
 
Registered: Apr 2005
Posts: 5,814

Rep: Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001
First of all, I would like to say thanks, as my thread inspired you to do this LOL

But seriously now, I have to run this and time it once I get home to see, if it would ever break, how LONG this would take. Thanks for giving me something to do with my time!

And also, if I think of anything to make it better, I will let you know.

Josh
 
Old 03-15-2011, 03:24 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Based on the following:
Quote:
$RANDOM is an internal Bash function (not a constant) that returns a pseudorandom [1] integer in the range 0 - 32767.
Yes to eventually being equal at least after this maximum is reached.

As to:
Quote:
Is there anything wrong (syntactically and/or logically) with the script?
1. Zero indenting which makes it quite difficult to read
2. C is an integer and not a string so quoting would not seem appropriate although it of course causes no error
3. (()) is used by bash for numeric comparisons and arithmetic so the following changes could be made:
Code:
if [ $A -eq $B ]; then

if (( A == B )); then

let C++

((C++))
4. Personally I am not fond of infinite loops so I would probably have an actual test in the while loop.
 
  


Reply

Tags
bash scripting, infiniteloops, logic, random number, syntax


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash script? how to rename files in random way? lefty.crupps Linux - Software 15 06-08-2014 04:47 PM
[SOLVED] Bash Problems - Random Headers when starting bash in X androidd Slackware 3 01-13-2011 05:29 PM
killing child processes of a bash script results in strange random kills omnio Programming 6 03-12-2007 08:35 AM
KDE Random wallpaper or script to create symbolic links to random files cvelasquez Linux - Software 2 02-26-2007 07:48 PM
BASH script showing random behavior chatterbug89 Programming 12 07-19-2005 11:45 PM


All times are GMT -5. The time now is 10:01 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration