LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Script Random Variable (https://www.linuxquestions.org/questions/programming-9/shell-script-random-variable-4088/)

Daniel 07-09-2001 01:25 PM

Shell Script Random Variable
 
Ive been writing games for my own personal pleasure in unix shell script, ive been cutting the seconds column off of date inorder to produce a random effect, is there a better way to get a random variable?

crabboy 07-09-2001 05:30 PM

The RANDOM shell variable can be used for random numbers once seeded. This is from the bash man page:
Quote:

RANDOM Each time this parameter is referenced, a random
integer between 0 and 32767 is generated. The
sequence of random numbers may be initialized by
assigning a value to RANDOM. If RANDOM is unset,
it loses its special properties, even if it is sub-
sequently reset.
This will seed the RANDOM with the date from 1/1/1970 in seconds, and generate a random number from 1 to 60.

Code:

RANDOM=`date '+%s'`
echo $[($RANDOM % 60) + 1]

Gary

Daniel 07-09-2001 05:49 PM

Quote:

date: bad format character - s
random[2]: syntax error at line 2 `(' is not expected.

crabboy 07-10-2001 09:25 PM

What type of system are your on, Linux? Which shell are you using. The example I gave was for bash.

Code:

#!/bin/sh

RANDOM=`date '+%s'`
echo $[($RANDOM % 60) + 1]
echo $[($RANDOM % 60) + 1]
echo $[($RANDOM % 60) + 1]

Look at the man page for your 'date' command. Is there a print option for 's' ( date in seconds)?

Daniel 07-11-2001 10:16 AM

Not linux Unix...

jharris 07-11-2001 10:44 AM

Quote:

Originally posted by Daniel
Not linux Unix...
Same difference as far as the shell in concerned. What shell you using?

cheers

Jamie...

Daniel 07-12-2001 12:30 PM

I dont really know what shell Im in. I would guess bash.. is there a way to find out?

crabboy 07-12-2001 09:58 PM

Try:

echo $SHELL

or

cat /etc/passwd | grep username

Where username is your login name on the machine. Look at the last entry. It sould be your shell.

unSpawn 07-15-2001 10:09 AM

maybe echo $RANDOM can help

Simmo512 11-30-2007 07:39 AM

1 - 9 aren't picked as much as 10 - 60?
 
I ran the code below on Linux in a loop and found that the output is indeed random between 10 and 60, but isn't between 1 and 9. Anyone care to explain why this is and if there is a way to get a true random value between say 0 and 60?


#!/bin/bash

RANDOM=`date '+%s'`

while true ; do
x=$[ ($RANDOM % 60) + 1 ]
echo $x >> /tmp/$x
done



Looking at the results with the command below always shows 1 to 9 getting the lowest hits.

ls -Sl /tmp

H_TeXMeX_H 11-30-2007 02:01 PM

For true random numbers you must use '/dev/urandom' like this:

Code:

dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -f1 -d" "
source: http://linuxgazette.net/issue55/tag/4.html

That's the neatest way of doing it :) (heck I don't even know what it's doing).

My way is just use python:
Code:

import os
import random

# seed the random number generator with urandom,
# basically just from /dev/urandom
random.seed(os.urandom)

# some random numbers 0-100
random.randint(0,100)

Now, that's much more readable :)

Hko 11-30-2007 03:44 PM

Quote:

Originally Posted by Simmo512 (Post 2975546)
Looking at the results with the command below always shows 1 to 9 getting the lowest hits.

ls -Sl /tmp

You're mearurement is broken:

You write the number itself to a file. When you write a single digit number (1-9) only a single character is written to the file. So the file sizes for single digit number grows twice as slow as for two-digit numbers (then two characters are written at one hit).

Then you sort the files by file size with "ls -lS"...

Hko 11-30-2007 04:00 PM

To make use of /dev/urandom in bash scripts I wrote this C program once:
Code:

/* devrandom - shell utility for generating random numbers using /dev/urandom */

/* public domain */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


#define DEVFILE "/dev/urandom"


int main(int argc, char *argv[])
{
    int fd;
    int result;
    int min, max;
    unsigned long long int number;

    /* read command line arguments */
    if (argc == 3) {
        min = atoi(argv[1]);
        max = atoi(argv[2]);
    } else if (argc == 2) {
        min = 0;
        max = atoi(argv[1]);
    } else {
        puts("Usage: devrandom [[min] max]");
        return 2;
    }

    /* read a bytes from /dev/urandom device file */
    fd = open(DEVFILE, O_RDONLY);
    if (fd < 0) {
        perror("");
        return 1;
    }
    result = read(fd, &number, sizeof number);
    if (result < 0) {
        perror("");
        return 1;
    }
    number >>= 1; /* no negative numbers by making most-significant bit 0 */
    number = number % (max - min + 1) + min;
    printf("%lld\n", number);
    return 0;
}


bigearsbilly 12-03-2007 03:55 AM

Quote:

random between 10 and 60, but isn't between 1 and 9. Anyone care to explain why this is
yes, you have done something wrong!


whenever you think you have found a unix bug, you almost certainly have NOT!

all these tools have been around for years and it is unlikely that you have
found a feature.

so always double check your conclusion.
;)

Simmo512 12-03-2007 05:00 AM

Random issue
 
Thanks for the replies.

You're absolutely right, the way I was measuring the success of my script was wrong. Once I wrote out the same single character for all files, true random values returned throughout.

Thanks for your time.

Simmo


All times are GMT -5. The time now is 11:32 AM.