LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-29-2007, 09:48 PM   #1
jaepi
Member
 
Registered: Apr 2007
Location: Urban Jungle
Distribution: Ubuntu
Posts: 189
Blog Entries: 1

Rep: Reputation: 30
how do i produce a single digit random number in BASH?


im currently practicing bash scripting and i want to create a flash card producing random numbers and random mathematical operations...im using $RANDOM, but it gives me bigger numbers like 15067...what is the "magic word" that will give a random number but not in 2 to 1 gazillion digits or whatsoever...thanks...

Last edited by jaepi; 04-29-2007 at 09:49 PM.
 
Old 04-29-2007, 11:19 PM   #2
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,476

Rep: Reputation: 189Reputation: 189
Here's a script called "rand" that I picked up somewhere a long time ago. It will print out random numbers between 0 and whatever upper bound(inclusive) you set:

Code:
#!/bin/bash
#< Generate a random number between zero and an upper bound inclusive
# KW 26/11/04

if [ "$#" -gt "1" ]; then
  echo "Usage: $(basename $0) [upper_bound]" && exit 1
elif [ "$#" -lt "1" ]; then
  upper=10
else
  echo ${1} | grep '^[0-9]*$' >/dev/null 2>&1
  if [ "$?" -eq "0" ]; then
     upper=${1}
  else
     echo "Argument must be numeric"
     exit 2
  fi
fi

awk -vu=$upper 'BEGIN {
   srand();
   printf( "%0.0f\n", rand() * u );
}'

exit 0
The syntax for random digits between 0 and 9 would be:

$ rand 9
 
Old 04-29-2007, 11:29 PM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,580
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
Or you could just do something like:

MYRAND=`$echo $RANDOM % 7 |bc`
echo $MYRAND

The above devides the $RANDOM number by 7 then prints only the remainder. Since the $RANDOM is random the remainder will always be random as well. You could use another single digit number other than 7 (I picked that one at random ) MYRAND would be the random number you got.
 
Old 04-29-2007, 11:41 PM   #4
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
Even better - bash has math support:

Code:
echo "$(($RANDOM % 10))"
Will print a random number between 0 and 9 inclusive (a single digit).
 
Old 04-29-2007, 11:43 PM   #5
jaepi
Member
 
Registered: Apr 2007
Location: Urban Jungle
Distribution: Ubuntu
Posts: 189
Blog Entries: 1

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by kilgoretrout
Here's a script called "rand" that I picked up somewhere a long time ago. It will print out random numbers between 0 and whatever upper bound(inclusive) you set:

Code:
#!/bin/bash
#< Generate a random number between zero and an upper bound inclusive
# KW 26/11/04

if [ "$#" -gt "1" ]; then
  echo "Usage: $(basename $0) [upper_bound]" && exit 1
elif [ "$#" -lt "1" ]; then
  upper=10
else
  echo ${1} | grep '^[0-9]*$' >/dev/null 2>&1
  if [ "$?" -eq "0" ]; then
     upper=${1}
  else
     echo "Argument must be numeric"
     exit 2
  fi
fi

awk -vu=$upper 'BEGIN {
   srand();
   printf( "%0.0f\n", rand() * u );
}'

exit 0
The syntax for random digits between 0 and 9 would be:

$ rand 9
uhhhmmm..how do i use this script in my flash card script?
should i create a function and place this as the content??
 
Old 04-30-2007, 12:19 AM   #6
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,476

Rep: Reputation: 189Reputation: 189
I would just create the rand script separately and put either the script or a link to the rand script in PATH. Then you could just call rand in your flashcard script when needed.
 
Old 04-30-2007, 12:37 AM   #7
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 122Reputation: 122
I would definitely use the BASH-only syntax. It will have much better support. zhangmaike seems to have the simplest system.
 
Old 04-30-2007, 12:43 AM   #8
jaepi
Member
 
Registered: Apr 2007
Location: Urban Jungle
Distribution: Ubuntu
Posts: 189
Blog Entries: 1

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by kilgoretrout
I would just create the rand script separately and put either the script or a link to the rand script in PATH. Then you could just call rand in your flashcard script when needed.
oh, so bash scripting supports external file linking? like, you create a different file for the fucntion then you call it through it's path?? did i get what you mean? how do i link them through paths?
 
Old 04-30-2007, 12:54 AM   #9
johndoe0028
Member
 
Registered: Jun 2005
Location: CA, USA
Distribution: Gentoo
Posts: 165

Rep: Reputation: 30
Source the external script with .

So if that script was /home/user/rand.sh, you could add a line:
. /home/user/rand.sh
and it would bring in that script's contents.
 
  


Reply


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 Random Number joelhop Programming 6 12-18-2006 12:13 PM
pls help on how i can generate 4 digit random number that are unique within the set icylicious Programming 7 08-22-2006 02:00 PM
can bash generate random digit? sorno Linux - Newbie 5 08-17-2005 07:05 AM
Produce random character with c program liguorir Linux - Software 4 05-23-2004 07:30 PM


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