LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 04-29-2016, 09:54 PM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311
Blog Entries: 2

Rep: Reputation: 16
Question Any rand() / srand() examples?


I am learning bash programming language. I would like to better understand the following command in line 6 of Example 9-17 Pseudorandom numbers, using awk (http://www.tldp.org/LDP/abs/html/randomvar.html).
Code:
srand(); print rand()
I try this out in terminal
Code:
$ srand(); print rand()
srand(); print rand()
bash: syntax error near unexpected token `;'
$ srand(5); print rand()
srand(5); print rand()
bash: syntax error near unexpected token `5'
$ srand(); echo $RANDOM
srand(); echo $RANDOM
bash: syntax error near unexpected token `;'
I search (ixquick) the web to only find the following unrelated articles:
http://www.thegeekstuff.com/2010/03/...-in-functions/ (isn't about "srand()", it's about awk)
http://stackoverflow.com/questions/6...g-it-with-unix
https://www.guyrutenberg.com/2007/09/03/seeding-srand/ (C/C++)
https://linuxconfig.org/bash-scripting-tutorial
http://www.delorie.com/gnu/docs/gawk/gawk_134.html (examples don't work)

I looked in the manual pages, rand has no examples, and srand's manual pages have a c programming example. It appears srand is C and not bash programming language.

Do you have to learn awk and gawk before learning some way of using rand() and srand(). Does anyone know how to use this? Any examples? The more simple the example the better.

Last edited by andrew.comly; 04-29-2016 at 10:00 PM.
 
Old 04-30-2016, 12:32 AM   #2
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,621
Blog Entries: 40

Rep: Reputation: Disabled
[QUOTE=andrew.comly;5538519]I am learning bash programming language. I would like to better understand the following command in line 6 of Example 9-17 Pseudorandom numbers, using awk (http://www.tldp.org/LDP/abs/html/randomvar.html).
Code:
srand(); print rand()
That is not the authentic line from your example code! It should be
Code:
AWKSCRIPT=' { srand(); print rand() } '
and has a different meaning than that of a command to execute on a command-line.

Above, in the introductory comment, it is written
Quote:
# Uses the awk rand() function.
To the variable AWKSCRIPT, is assigned a line of text ' { srand(); print rand() } ' which comprises 2 commands that awk will execute. It does not make sense to have bash execute awk-scripts.

In the previous examples, notably before very first one on the page, the internal Bash function $RANDOM is mentioned. It should now be obvious which command needs awk and which one will run on the bash command-line.

This mystery sorted out, you can lookup both ways to create random numbers in the man-pages.

man awk (then search for rand() )
man bash (then search for RANDOM)
 
1 members found this post helpful.
Old 04-30-2016, 09:52 AM   #3
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moderator response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
1 members found this post helpful.
  


Reply

Tags
random, terminal



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
[SOLVED] Perl rand / srand function deelinux Programming 2 10-19-2015 03:38 AM
[SOLVED] srand(static_cast<unsigned int>(clock( ))); seems not random enough fsshl Programming 2 07-21-2011 07:31 PM
[C++] Different output from same input ( without srand() ) sylvaticus Programming 2 02-09-2008 12:30 PM
srand(), rand(), RAND_MAX, or XXX weirdness nazdrowie Programming 2 03-07-2005 12:45 PM
srand in c suchi_s Programming 14 07-27-2004 01:14 PM

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

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