LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-09-2013, 09:16 PM   #1
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Rep: Reputation: 47
Randomly choose 10, 100, 1000 or 10000


I am trying to choose randomly 10, 100, 1000 or 10000. I am using following code which does not work:
Code:
DIGITS=$[ ( $RANDOM % 4 )  + 1 ]
echo "Choice= $[ ( 10 ^ $DIGITS ) ]"
Where is the error? Thanks for your help.
 
Old 04-10-2013, 12:30 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
which language is it?
 
Old 04-10-2013, 12:41 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Wrong type of brackets and exponential operator; try this
Code:
digits=$((RANDOM%4 +1))
echo $digits
echo $((10 ** digits))
http://tldp.org/LDP/abs/html/ops.html
 
1 members found this post helpful.
Old 04-10-2013, 12:51 AM   #4
eklavya
Member
 
Registered: Mar 2013
Posts: 636

Rep: Reputation: 142Reputation: 142
@chrism01
EXCELLENT !!!

I was doing something like this because OP didn't mention script language.
Code:
number=$(python -c 'import random; print(random.randint(1, 4))')
echo $((10 ** $number))
But your solution is accurate because OP asked
Code:
Where is the error?

Last edited by eklavya; 04-10-2013 at 01:11 AM.
 
Old 04-10-2013, 03:20 AM   #5
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Original Poster
Rep: Reputation: 47
Many thanks for your replies. It is linux bash script. Just changing the operator from '^' to '**' works well.

How should you choose between using '[ ]' and '( )' (and '{ }')
 
Old 04-10-2013, 07:55 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
For simplicity and clarity, I'd define a constant array with these four values in it, then choose a random index (0..3) to pick from that array.

Now, it's obvious what the choices will be, and they no longer have to be a power-of-ten. They could be anything.
 
Old 04-10-2013, 08:02 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Re bracket types:

1. use [[ .. ]] for string comparisons (optionally also numeric comparisons)
http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS

2. use (( )) for doing arithmetic and doing numeric comparisons

3. use {} for varnames embedded in strings eg var v2 is embedded in strings below
Code:
v1=asd${v2}df

v1=asdf${v2}

v1=${v2}asd
Enables the parser to pick out the var from the surrounding chars

Have a read of
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/
 
Old 04-10-2013, 11:42 AM   #8
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Original Poster
Rep: Reputation: 47
Thanks for clarifying. Apparently there is no need to combine brackets like '[( code )]'
 
Old 04-10-2013, 09:27 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Forgot ( )
4. $(cmd) used usually to assign cmd results to a var eg
v1=$(cmd)


Combined sets of different bracket types are used in certain situations eg arrays http://tldp.org/LDP/abs/html/arrays.html, regexes http://stackoverflow.com/questions/1...-regex-in-bash etc.

Last edited by chrism01; 04-10-2013 at 09:30 PM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find out my NIC speed (10/100/1000) fjkum Linux - Networking 4 07-13-2006 09:47 PM
gigabit 10/100/1000 onboard lan on soltek k8tpro mortal Slackware 3 12-10-2005 08:45 AM
Micronet SP 2610/2612 10/100/1000 card shahriar *BSD 0 04-02-2004 03:58 AM
Ethernet 3Com Gigabit 10/100/1000 _NYTRO_ Linux - Software 3 11-13-2003 06:28 PM
Nics 100 / 1000 NSKL Linux - General 5 02-13-2002 09:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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