LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-27-2011, 04:43 PM   #1
pharaoh19
LQ Newbie
 
Registered: Dec 2010
Posts: 3

Rep: Reputation: 0
Random float numbers in BASH


Hi people

I'm learning shell scripting using bash and I want to generate 4 floating point number with 5 decimal places and write them to a file and a variable. I've done all this except the $RAMDOM enviroment variable does not generate a float number but a integrer.

I hope you could explain why to lead me to a solution.
Thnak you all in advance.

PS: script so far
Code:
#!/bin/bash

#: Title		: randomFloats
#: Date			: 2011-03-27
#: Author		: pharaoh
#: Version		: 1.0 
#: Discription	        : Generate a 4 float numbers with 5 decimal places and write them to a file and a variable 


listFloats=0.0
for i in $(seq 0 3)
do
    aux=$[($RANDOM % 15000)]
	printf -v aux "%.5f" ${aux}
	
	#In the first iteration it just set the variable, in the others it append
	if [ $i -eq 0 ]
	then
		listFloats=${aux}
		printf ${aux} > randomFloats.txt
	else 
		listFloats="${listFloats} ${aux}"
		printf ${aux} >> randomFloats.txt
	fi
done

printf "%s\n" ${listFloats}
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-27-2011, 05:12 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Note that $RANDOM range is [0, 32767] (not enough for five digits), and Bash does not have any support for floating-point arithmetic. You'll need to use fixed-point arithmetic or string manipulation, if you really want to do this with Bash.

A random number [0.00000, 1.00000):
Code:
v=$[(100 + (RANDOM % 100)]$[1000 + (RANDOM % 1000)]
v=0.${v:1:2}${v:4:3}
echo $v
Or, [0.00000, 15.00000):
Code:
v=$[100 + (RANDOM % 100)]$[1000 + (RANDOM % 1000)]
v=$[RANDOM % 15].${v:1:2}${v:4:3}
echo $v
Rather than Bash, I'd use e.g. awk for floating-point calculation and simple data manipulation:
Code:
awk 'BEGIN { printf("%.5f\n", rand() * 15.0) }'
although awk's rand() needs a seed via srand(), e.g.
Code:
awk -v "seed=$[(RANDOM & 32767) + 32768 * (RANDOM & 32767)]" \
       'BEGIN { srand(seed); printf("%.5f\n", rand() * 15.0) }'
 
1 members found this post helpful.
Old 03-27-2011, 07:26 PM   #3
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
I recommend not using shell. Either one of the big P's or R (Python|Perl|PHP|Ruby) will do this easily for you

Ruby1.9+ (for random floats from 0 to 5)
Code:
$ ruby -e '1.upto(5){print "%.5f\n" % (rand * 5 + 0.0) }'
3.27893
2.63196
3.81157
0.46412
2.72308
 
1 members found this post helpful.
Old 03-27-2011, 07:43 PM   #4
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
You could also use bc:

MULTIPLIER=10 # generates from 0 - 9.99999
echo "scale=5; $RANDOM*$MULTIPLIER/32767" | bc
 
2 members found this post helpful.
Old 03-28-2011, 08:48 AM   #5
pharaoh19
LQ Newbie
 
Registered: Dec 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Thank you guys!
 
  


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
Random numbers in C loke137 Programming 9 09-11-2010 10:37 AM
Random numbers between -5.0 to +5.0 smp Linux - Newbie 12 08-17-2010 06:57 PM
random numbers ovince Programming 5 06-04-2007 02:47 AM
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

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

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