LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-23-2021, 11:26 AM   #1
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 16,314

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Random numbers in scripts


Code:
bash-5.1$ min=0; max=15; number =$(expr $min + $RANDOM % $max); echo $number
number: illegal number: =6
I'm no expert in scripts, they're definitely ugly and obtuse to my mind, but why is every number illegal? I simply want a pseudo random generator between two known figures.
 
Old 02-23-2021, 11:32 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,268
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Quote:
Originally Posted by business_kid View Post
Code:
bash-5.1$ min=0; max=15; number =$(expr $min + $RANDOM % $max); echo $number
number: illegal number: =6
I'm no expert in scripts, they're definitely ugly and obtuse to my mind, but why is every number illegal? I simply want a pseudo random generator between two known figures.
Because of the space here:

Code:
...number =$(expr...
No space is allowed between the var name and the equal sign.

It is not the number that is illegal, it is the assignment syntax.
 
Old 02-23-2021, 11:34 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,719

Rep: Reputation: 5912Reputation: 5912Reputation: 5912Reputation: 5912Reputation: 5912Reputation: 5912Reputation: 5912Reputation: 5912Reputation: 5912Reputation: 5912Reputation: 5912
Code:
 min=0; max=15; number =$(expr $min + $RANDOM % $max); echo $number
You have a space between number and =

To slow...
 
Old 02-24-2021, 04:25 AM   #4
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 16,314

Original Poster
Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Thanks. This worked
Code:
number=$(expr $min + $RANDOM % $max)
 
Old 02-24-2021, 05:36 AM   #5
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Pretty much every POSIX-compatible shell has arithmetic expansion nowadays. No need for expr here.
Code:
number=$((min + RANDOM % max))
 
Old 02-24-2021, 06:54 AM   #6
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 16,314

Original Poster
Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Yeah, that works too. Thanks shruggy.

FYI, the random function in bash is poor. I set this at 0-15, and ran it for a few minutes. I didn't count, but about 5 numbers repeated, 5 came occasionally, and 5 never showed. It's interesting nevertheless
 
Old 02-24-2021, 08:08 AM   #7
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Perhaps /dev/urandom would give better results?
Code:
echo $((0+$(hexdump -n1 -e'"%u"' /dev/urandom)%15))
 
Old 02-24-2021, 12:08 PM   #8
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 16,314

Original Poster
Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Thx shruggy. It is better, but still not great. I thought havegd provided a good random seed. But I am writing something and will simply find another illustration. I'll have to wipe a paragraph. The problem of logically producing complete randomness has no perfect answers - yet.
 
Old 02-24-2021, 02:28 PM   #9
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Another possibility is shuf
Code:
number=$(shuf -i0-15 -n1)
An obligatory xkcd reference: https://xkcd.com/221

Last edited by shruggy; 02-26-2021 at 10:06 AM.
 
  


Reply

Tags
random, shell



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
LXer: Security is weakened because random numbers are not random enough LXer Syndicated Linux News 0 08-10-2015 01:20 PM
[SOLVED] sed - replacing random numbers followed with space and more random chars gluposti Linux - Newbie 7 05-07-2012 07:36 PM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 01:42 PM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 12:12 PM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 09:18 AM

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

All times are GMT -5. The time now is 12:53 AM.

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