ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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)
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"...
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;
}
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.