/dev/urandom


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers /dev/urandom
# 8  
Old 01-31-2007
If you just want a random number between 1 and 100, using bash...
Code:
$ echo $((((100*RANDOM)/32767)+1))
55

# 9  
Old 02-01-2007
Got it, thanks!
# 10  
Old 02-01-2007
Quote:
Originally Posted by Ygor
If you just want a random number between 1 and 100, using bash...
Code:
$ echo $((((100*RANDOM)/32767)+1))
55

echo $((RANDOM%100+1))
# 11  
Old 02-01-2007
Quote:
Originally Posted by sb008
echo $((RANDOM%100+1))
That is a poor method since it will return a random number in the range 1..68 more often than 69..100.
# 12  
Old 02-02-2007
Quote:
Originally Posted by Ygor
That is a poor method since it will return a random number in the range 1..68 more often than 69..100.
While it is true that the modulus operation will return numbers in the range 1-68 more often than 69-100, this is not the reason that modulus should be avoided to change the range of random number generator. First about the range... the bash man page says:
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 subsequently reset.
That is 32768 possible values and you cannot distribute that in 100 bins evenly. Your algorithm will take an input of 32767 and return 101, so I tried to modify it a bit to see if I could fix that. Here is a script that shows the distributions from sb008's algorithm, my algorithm, your algorithm, and my modification to your algorithm:
Code:
#! /usr/bin/ksh
typeset -R3 fnum

function format
{
        typeset i=$1
        result=""
        ((i>101)) && return 0
        fnum=$i
        result="${result}${fnum} "
        fnum=${sb008[i]}
        result="${result}${fnum} "
        fnum=${perd[i]}
        result="${result}${fnum} "
        fnum=${ygor1[i]}
        result="${result}${fnum} "
        fnum=${ygor2[i]}
        result="${result}${fnum} "
        return 0
}

i=0
while ((i< 103)) ; do
        sb008[i]=0
        perd[i]=0
        ygor1[i]=0
        ygor2[i]=0
        ((i=i+1))
done
random=0
while ((random < 32768)) ; do
        a=$(((random%100)+1))
        ((sb008[a]=sb008[a]+1))
        a=$(((random/(32767/99))+1))
        ((perd[a]=perd[a]+1))
         a=$((((100*random)/32767)+1))
        ((ygor1[a]=ygor1[a]+1))
         a=$((((99*random)/32767)+1))
        ((ygor2[a]=ygor2[a]+1))
        ((random=random+1))
done
i=0
while ((i<27)) ; do
        line=""
        format $i
        line="${result}"
        format $((i+27))
        line="${line}      ${result}"
        format $((i+54))
        line="${line}      ${result}"
        format $((i+81))
        line="${line}      ${result}"
        echo "$line"
        ((i=i+1))
done

exit 0

  0   0   0   0   0        27 328 330 328 331        54 328 330 328 331        81 327 330 328 331
  1 328 330 328 331        28 328 330 327 331        55 328 330 327 331        82 327 330 327 331
  2 328 330 328 331        29 328 330 328 331        56 328 330 328 331        83 327 330 328 331
  3 328 330 328 331        30 328 330 328 331        57 328 330 328 331        84 327 330 328 331
  4 328 330 327 331        31 328 330 327 331        58 328 330 327 331        85 327 330 327 331
  5 328 330 328 331        32 328 330 328 331        59 328 330 328 331        86 327 330 328 331
  6 328 330 328 331        33 328 330 328 331        60 328 330 328 331        87 327 330 328 331
  7 328 330 327 331        34 328 330 327 331        61 328 330 327 331        88 327 330 327 331
  8 328 330 328 331        35 328 330 328 331        62 328 330 328 331        89 327 330 328 331
  9 328 330 328 331        36 328 330 328 331        63 328 330 328 331        90 327 330 328 331
 10 328 330 327 331        37 328 330 327 331        64 328 330 327 331        91 327 330 327 331
 11 328 330 328 331        38 328 330 328 331        65 328 330 328 331        92 327 330 328 331
 12 328 330 328 331        39 328 330 328 331        66 328 330 328 331        93 327 330 328 331
 13 328 330 327 331        40 328 330 327 331        67 328 330 327 331        94 327 330 327 331
 14 328 330 328 331        41 328 330 328 331        68 328 330 328 331        95 327 330 328 331
 15 328 330 328 331        42 328 330 328 331        69 327 330 328 331        96 327 330 328 331
 16 328 330 327 331        43 328 330 327 331        70 327 330 327 331        97 327 330 327 331
 17 328 330 328 331        44 328 330 328 331        71 327 330 328 331        98 327 330 328 331
 18 328 330 328 331        45 328 330 328 331        72 327 330 328 331        99 327 330 328 330
 19 328 330 327 331        46 328 330 327 331        73 327 330 327 331       100 327  98 327   1
 20 328 330 328 331        47 328 330 328 331        74 327 330 328 331       101   0   0   1   0
 21 328 330 328 331        48 328 330 328 331        75 327 330 328 331
 22 328 330 327 331        49 328 330 327 331        76 327 330 327 331
 23 328 330 328 331        50 328 330 328 330        77 327 330 328 331
 24 328 330 328 331        51 328 330 328 331        78 327 330 328 331
 25 328 330 327 331        52 328 330 327 331        79 327 330 327 331
 26 328 330 328 331        53 328 330 328 331        80 327 330 328 331

As for why the modulus operation is not recommended, it tends to discard the most significant bits while our algorithms tend to discard the least significant bits. With many generators, the least significant bits are not very random. This is particularly true of linear congruential generators which is probably what bash uses.
# 13  
Old 02-02-2007
Nice catch but wrong way to fix it. My post should have read...
Code:
echo $((((100*RANDOM)/32768)+1))

The modulus method is mathematically incorrect.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. HP-UX

Dev/urandom and dev/random missing in HP-UX

Hi, In our HP-UX B.11.11. I could not find dev/urandom and dev/random Are all pseudo-devices implemented as device drivers, or in need to run /configure some package to install the package to have dev/urandom. Please help (4 Replies)
Discussion started by: rashi
4 Replies

2. UNIX for Advanced & Expert Users

Cmd 'cat /dev/urandom' not closing cleanly

Hi I'm running the following command to generate a random password in a KSH script on a RHEL Linux VM but for some reason the cmd is not being closed and it's causing problems on the host. PASSWORD="$(cat /dev/urandom | tr -dc "a-zA-Z0-9" | fold -w 16 | head -1)Aa0!" The code worked as... (2 Replies)
Discussion started by: user052009
2 Replies

3. Red Hat

Changing grub from /dev/sda to /dev/sdb

Hi, Please suggest steps to change grub from /dev/sda to /dev/sdb, (1 Reply)
Discussion started by: manoj.solaris
1 Replies

4. Shell Programming and Scripting

Automating partitioning setup of /dev/sda on /dev/sdc

Objective: To recreate the partitioning setup of /dev/sda on /dev/sdc How would I parse the below information and initialize variables (an array?) that can be used to build sgdisk commands in a script, regardless of the number of partitions? Something along the lines of: sgdisk -n... (12 Replies)
Discussion started by: RogerBaran
12 Replies

5. AIX

Problem in /dev/hd1 and /dev/hd9var

Hello AIXians, I can't boot my AIX, it hangs and stops at the code error: 0518 After searching google, I knew the problem is due to problems in File Systems. So the solution is booting from any bootable media, then run these commands in maintenance mode: #fsck -y /dev/hd4 #fsck -y... (3 Replies)
Discussion started by: Mohannad
3 Replies

6. AIX

Difference between /dev/hdisk and /dev/rhdisk

Hi, How can i check that i am using RAW devices for storage in my AIX machine... Also after adding a LUN from storage to a aix host, when i check /dev in the host, i can see both rhdisk and hdisk with same number eg: dcback1(root):/dev>ls -lrt | grep disk12 crw------- 1 root ... (4 Replies)
Discussion started by: jibujacob
4 Replies

7. Solaris

Lun remove, stuck in /dev/dsk and /dev/rdsk

So, we removed a LUN from the SAN and the system is refusing to remove the references to it in the /dev folder. I've done the following: devfsadm -Cv powermt -q luxadm -e offline <drive path> luxadm probe All those commands failed to remove the path. The drive stills shows up as <drive... (13 Replies)
Discussion started by: DustinT
13 Replies

8. Solaris

What is /dev/tty /dev/null and /dev/console

Hi, Anyone can help My solaris 8 system has the following /dev/null , /dev/tty and /dev/console All permission are lrwxrwxrwx Can this be change to a non-world write ?? any impact ?? (12 Replies)
Discussion started by: civic2005
12 Replies
Login or Register to Ask a Question