LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 07-09-2001, 02:25 PM   #1
Daniel
Member
 
Registered: Feb 2001
Location: Oregon
Distribution: Mandrake
Posts: 68

Rep: Reputation: 15
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?
 
Old 07-09-2001, 06:30 PM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
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
 
Old 07-09-2001, 06:49 PM   #3
Daniel
Member
 
Registered: Feb 2001
Location: Oregon
Distribution: Mandrake
Posts: 68

Original Poster
Rep: Reputation: 15
Quote:
date: bad format character - s
random[2]: syntax error at line 2 `(' is not expected.
 
Old 07-10-2001, 10:25 PM   #4
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
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)?
 
Old 07-11-2001, 11:16 AM   #5
Daniel
Member
 
Registered: Feb 2001
Location: Oregon
Distribution: Mandrake
Posts: 68

Original Poster
Rep: Reputation: 15
Not linux Unix...
 
Old 07-11-2001, 11:44 AM   #6
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

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

cheers

Jamie...
 
Old 07-12-2001, 01:30 PM   #7
Daniel
Member
 
Registered: Feb 2001
Location: Oregon
Distribution: Mandrake
Posts: 68

Original Poster
Rep: Reputation: 15
I dont really know what shell Im in. I would guess bash.. is there a way to find out?
 
Old 07-12-2001, 10:58 PM   #8
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
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.
 
Old 07-15-2001, 11:09 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,810
Blog Entries: 54

Rep: Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986
maybe echo $RANDOM can help
 
Old 11-30-2007, 08:39 AM   #10
Simmo512
LQ Newbie
 
Registered: Nov 2007
Posts: 19

Rep: Reputation: 0
Thumbs up 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
 
Old 11-30-2007, 03:01 PM   #11
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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
 
Old 11-30-2007, 04:44 PM   #12
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by Simmo512 View Post
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"...
 
Old 11-30-2007, 05:00 PM   #13
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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;
}

Last edited by Hko; 11-30-2007 at 05:01 PM.
 
Old 12-03-2007, 04:55 AM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
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.
 
Old 12-03-2007, 06:00 AM   #15
Simmo512
LQ Newbie
 
Registered: Nov 2007
Posts: 19

Rep: Reputation: 0
Smile 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
 
  


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
Shell Script and Dynamic variable xanthium Programming 11 07-12-2011 07:05 AM
can a C function return value to Shell Script variable yarnar Programming 17 06-02-2010 06:54 PM
Random Name Shell Script tebucky Linux - Newbie 2 04-27-2005 01:47 PM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 08:47 AM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 01:12 PM


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