Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

random number generator

Status
Not open for further replies.
Joined
Jul 7, 2010
Messages
21
Location
DE
I am a beginner with bash/awk etc. I generate a number with the script below that works

1. I would like to round this number up
2. I would like to declare a variable that will have the value of the rounded up number.


can somebody please help or provide a link?thanks in advance


AWKSCRIPT=' { srand(); print rand()*10000 } '
echo | awk "$AWKSCRIPT"
 

Try:
Code:
AWKSCRIPT=' { srand(); print rand()*10000 } '
randVar=`awk "$AWKSCRIPT"`
[3eyes]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
thanks for the post..I tried ur script and some variations but unfortunately it doesnt seem to work
 
Is $RANDOM not good enough for you?

Otherwise, try:

Code:
awk 'BEGIN { srand(); printf("%d\n",rand()*10000) }'

Annihilannic.
 
this works great!

any tip how I can get the output value in a variable that I can further use?

$RANDOM was what I first tried but I couldn't make it work..

thanks!

 
Code:
myvar=$(awk 'BEGIN { srand(); printf("%d\n",rand()*10000) }')

Which shell are you using, perhaps $RANDOM isn't implemented in it?

Annihilannic.
 
so thankful!!!!

does the line below say anything concerning the shell I am using?
#!/bin/sh

or this one?
BusyBox v1.13.4 (2009-10-10 01:39:56 CEST) built-in shell (ash)

I am working on a wifi router after I installed this linux firmware/OS:
 
I see, it's an embedded system, so the shell probably has minimal functionality to save memory footprint.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top