Random Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Random Variable
# 1  
Old 08-04-2013
Random Variable

Hi,

Could you please let me know what the following code does, I know that it means generating random numbers, however not sure what is the entire purpose.

Code:
  R=$(($RANDOM % 2))
        delay=$(($RANDOM % 10))

        if [ $R -gt 0 ]
        then
                TEXT='X'
        else
                TEXT='F'
        fi

Thanks.
# 2  
Old 08-04-2013
Line 1 of YOUR code sets an integer random number R variable to 0 OR 1 dependant on the program run(time).
Line 2 does the same between 0 to 9 but in this code is wasted time as it effectively is not used and therefore does nothing...

The conditional statement looks for 0 as the crucial value from the variable R and sets the TEXT variable to "F".

ANY other value would give the TEXT variable as "X"...

Code:
#!/bin/bash

R=$(($RANDOM % 2))
echo "$R"
delay=$(($RANDOM % 10))
echo "$delay"
        if [ $R -gt 0 ]
        then
                TEXT='X'
                echo "$TEXT"
        else
                TEXT='F'
                echo "$TEXT"
        fi

I hope that is lucid enough...
This User Gave Thanks to wisecracker For This Post:
# 3  
Old 08-04-2013
it is just a digital version of a coin flip ... depending on the remainder after the random number generated is divided by 2, the code sets the value of the TEXT variable to X if the resulting number is greater than 0 or sets it to F if not ...
This User Gave Thanks to Just Ice For This Post:
# 4  
Old 08-05-2013
Thanks a Lot!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Random letters

Hi there, first of all this is not homework...this is a new type of exercise for practicing vocabulary with my students. I have a file consisting of two columns, separated by a tab, each line consisting of a word and its definition, separated by a line break. What i need is to replace a... (15 Replies)
Discussion started by: eldeingles
15 Replies

2. Homework & Coursework Questions

Random numbers

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! Write a shell script that will take the sum of two random number? Ex: Random n1 +Random n2 = result i tries to write it but i had some dufficulties ... (3 Replies)
Discussion started by: renegade755
3 Replies

3. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

4. UNIX for Dummies Questions & Answers

Random Crashing

Over the last month or so my CentOS server has been crashing for reasons I do not know. It has been running for over a year with regular yum updates without problems. The load on the server is perfectly normal with CPU usage at 5-6% and RAM usage at less than half of 32GB of RAM (multiple smaller... (3 Replies)
Discussion started by: spinner0205
3 Replies

5. Programming

random number

How can I choose randomly the row numbers of my file in awk? (4 Replies)
Discussion started by: Homa
4 Replies

6. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

7. Shell Programming and Scripting

$random

I need to use the $RANDOM command to get a line from a list of lines in a file randomly. file is help go three house film how do i randomly get one word without looking into the file? (6 Replies)
Discussion started by: relle
6 Replies

8. Shell Programming and Scripting

replacing a number with random variable inside shell script

Hi All. I need help for the below logic. I ve a file like following input file: NopTX(5) // should be remain same as input ----Nop(@100); //1 Nop(90); //2 --Nop(80); //3 @Nop(70); //4 --@Nop(60); //5 @Nop@(@50); //6 --Nop@( 40); ... (3 Replies)
Discussion started by: user_prady
3 Replies

9. Shell Programming and Scripting

Random

My problem is as follow and i hope you can help: I currently have this function: stored_word() { number=$RANDOM let "number %= 21" case $number in 0 ) echo "energy" ;; 1 ) echo "touch" ;; 2 ) echo "climbing" ;; 3 ) echo "declare" ;; 4 ) echo "marry" ;; 5 ) echo "relax" ... (8 Replies)
Discussion started by: keyvan
8 Replies

10. Programming

random ip address

suppose that my network client process does not possess an effective root UID. is it possible to change the client host machine ip address to a random number? (3 Replies)
Discussion started by: xtrix
3 Replies
Login or Register to Ask a Question