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 08-14-2020, 06:29 PM   #1
vmill
LQ Newbie
 
Registered: Sep 2006
Posts: 3

Rep: Reputation: 0
Unhappy Random


Not sure if programming forum is best but I am trying a simple script on Fedora for a random number generator. I know it is not truly random and am ok with a cheesy hack.

I searched and found "generate random number in specific range" examples:
generating a random number between 11 and 30: echo $[ $RANDOM % 20 + 11 ]

It seems to work, but where does 20 come into anything? In other examples I found, the first number is the high end of the range. If I wanted something random between 10 & 15 what would I use?

Confused
 
Old 08-14-2020, 06:48 PM   #2
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 427
Blog Entries: 4

Rep: Reputation: 189Reputation: 189
$RANDOM % 20 generates a random number betwwen 0 and 19, add 11 to get a number from 11 to 30

For a value between 10 & 15, use $[$RANDOM % 6 + 10]
 
1 members found this post helpful.
Old 08-14-2020, 06:53 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,764

Rep: Reputation: 4408Reputation: 4408Reputation: 4408Reputation: 4408Reputation: 4408Reputation: 4408Reputation: 4408Reputation: 4408Reputation: 4408Reputation: 4408Reputation: 4408
Quote:
Originally Posted by dogpatch View Post
For a value between 10 & 15, use $[$RANDOM % 6 + 10]
Which, I'll add, is:

Code:
echo $[RANDOM % ((15 - 10 + 1)) + 10]
That should make it completely clear.

Last edited by dugan; 08-14-2020 at 07:00 PM.
 
Old 08-14-2020, 07:39 PM   #4
vmill
LQ Newbie
 
Registered: Sep 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by dogpatch View Post
$RANDOM % 20 generates a random number betwwen 0 and 19, add 11 to get a number from 11 to 30

For a value between 10 & 15, use $[$RANDOM % 6 + 10]
I found a couple of pages that basically said the same thing (the example) and the man file, and not explaining how it was actually formed. I understand it now.

Thanks for the explanation.
 
Old 08-14-2020, 07:44 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 20,421

Rep: Reputation: 3556Reputation: 3556Reputation: 3556Reputation: 3556Reputation: 3556Reputation: 3556Reputation: 3556Reputation: 3556Reputation: 3556Reputation: 3556Reputation: 3556
Quote:
$RANDOM % X + Y
To elaborate on the previous posts basically $RANDOM outputs a signed 16 bit integer i.e a number between 0-32767

To output a smaller range you use the modulo function. The modulo function returns the remainder of an integer division operation. You would take the modulo using the value of the desired range then add the offset.

Quote:
$RANDOM % 6 + 1
As another example to output a random number from 1-6 you use 6 for the modulo which outputs 0-5 then add 1. A simpler function is shuf which does all the range calculations for you. For the same example

Quote:
shuf -i 1-6 -n 1
Will output a random number between 1-6 and display 1 number instead the default which is 20. See man pages for shuf for more details.

I see that you have figured it out...
 
1 members found this post helpful.
Old 08-15-2020, 06:09 AM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 922

Rep: Reputation: 214Reputation: 214Reputation: 214
% IS THE MODULO. ie % 20 returns integers between 0 and 19. Adding 11 to it, you get integers between 11 and 30.
 
Old 08-15-2020, 07:01 AM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 15,305
Blog Entries: 9

Rep: Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379
I was surprised to see the construct $[ ... ] in:
Code:
echo $[ $RANDOM % 20 + 11 ]
Since when is this supported? Is it a bash thing? Does it replace $(( ... )) only, or also other (non-arithmetic) constructs?
 
Old 08-15-2020, 12:40 PM   #8
GazL
LQ Guru
 
Registered: May 2008
Distribution: CRUX
Posts: 5,547
Blog Entries: 14

Rep: Reputation: 3388Reputation: 3388Reputation: 3388Reputation: 3388Reputation: 3388Reputation: 3388Reputation: 3388Reputation: 3388Reputation: 3388Reputation: 3388Reputation: 3388
$[ ] is an obsolete syntax used in early bash versions, that was superseded by $(( )).
 
1 members found this post helpful.
Old 08-16-2020, 03:07 AM   #9
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 15,305
Blog Entries: 9

Rep: Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379
^ Thank you!
 
Old 09-01-2020, 12:51 AM   #10
Fat_Elvis
Member
 
Registered: Oct 2016
Distribution: FreeDOS 1.2
Posts: 299

Rep: Reputation: 90
You can read directly from /dev/random (/dev/urandom if you need many bytes).
Code:
read -n1 RAND < /dev/urandom
printf "%q\n" ${RAND}
 
  


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
virus on random web pages at random intervals irian Linux - Security 13 11-17-2009 01:50 PM
Box is attempting to scan and ssh into random machines using random usernames gianh Linux - Security 2 11-09-2008 05:42 PM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 01:42 PM
KDE Random wallpaper or script to create symbolic links to random files cvelasquez Linux - Software 2 02-26-2007 06:48 PM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 12:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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