LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-16-2005, 11:18 PM   #1
sorno
LQ Newbie
 
Registered: Aug 2004
Posts: 21

Rep: Reputation: 15
can bash generate random digit?


i am currently using mac os x, i do "man rand" and i find that rand can do the job. However, when i use rand, it said command not find... is there anything i can do to get back my rand command? or is that other way to do it??
 
Old 08-16-2005, 11:27 PM   #2
PenguinPwrdBox
Member
 
Registered: Oct 2003
Posts: 568

Rep: Reputation: 30
Try:
Code:
echo $RANDOM
 
Old 08-17-2005, 12:32 AM   #3
sorno
LQ Newbie
 
Registered: Aug 2004
Posts: 21

Original Poster
Rep: Reputation: 15
thank you very much!!!
so there is no way to control the range??i have to use modulus??
 
Old 08-17-2005, 12:57 AM   #4
PenguinPwrdBox
Member
 
Registered: Oct 2003
Posts: 568

Rep: Reputation: 30
There maybe a way, however, if you need a larger number, you could maybe just use two, multiplied, added, whatever.
Maybe an algorithm whereby if the random is prime, generate another and multiply them....?

$RANDOM is a bash builtin. I dunno if you can alter it.
I tried typeset to change it - but no luck....
 
Old 08-17-2005, 03:35 AM   #5
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,605

Rep: Reputation: 53
See Example 9−29. Pseudorandom numbers, using awk
in the Advanced Bash−Scripting Guide
then x by whatever upper limit you may want

maybe you want to look at pinching numbers from /dev/urandom?

This srand / rand function in C (man srand, rand)
I have never figured out whether these functions (or any C function found in man) could
be called from bash :-( need to read more
 
Old 08-17-2005, 07:05 AM   #6
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
But you have to be careful with operations between random numbers, because you can change the distribution!
 
  


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
Bash - choose a random line from a file ericcarlson Linux - Software 27 11-12-2013 02:25 AM
Generate random number with C++ TruongAn Programming 5 11-09-2005 01:01 AM
Generate files using bash kaon Linux - General 1 03-08-2005 02:59 PM
Python problems trying to generate a random number davholla Programming 0 10-27-2003 05:07 AM
bash random function NSKL Programming 6 02-02-2003 02:39 PM


All times are GMT -5. The time now is 03:50 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration