genrating pseudo random numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting genrating pseudo random numbers
# 8  
Old 04-27-2007
Here is a script to generate random numbers between 0 and 2^31-1. You will need to modify it yourself to fully meet your needs.
Code:
#! /usr/bin/ksh



#
# Set up a bc coprocess

bc |&
print -p ibase=16

#########################################################################
####  Tigershark ---   a portable and powerful RNG
#
#  This is a Recusion With Carry generator.  It has an output modulus
#  of 32768 and a period of 9,194,221,792,641,269,759.

typeset -u Entropy ZeroEntropy
ZeroEntropy="0000000000000000000000000000000000000000"
Entropy=$ZeroEntropy

typeset -i10 X3 X2 X1 X0 Carry Sum
Period=9194221792649674751
Carry=0
Sum=0
Ttigershark=0
Stigershark=0
# Sum = (Word) the accumulator
# Carry = carry
# Ttigershark = Total calls
# Stigershark = Total times started


#
#  The function is now mostly for reference.  The 7 lines inside the
#  the function are used in most places where the function was once called.
#  This improves speed.

function tigershark
{
    ((Sum=15*X3 + 31111*X2 + 15*X1 + 1064*X0 + Carry ))
    ((Carry=Sum >> 15))
    ((X0=Sum & 16#7FFF ))
        X3=$X2
        X2=$X1
        X1=$X0
        ((Ttigershark=Ttigershark+1))
        return 0
}

#
#  weak_start_tigershark --- This is used when we can't get Entropy

function weak_start_tigershark
{
        typeset -i  i final duration
        ((final=SECONDS+2))

        while((SECONDS < final)) ; do
                i=$RANDOM
        done

        ((Carry=0))
        ((X0=$$))
        ((X1=RANDOM))
        ((X2=RANDOM))
        ((X3=RANDOM))
        Sum=0
        ((Stigershark=Stigershark+1))
        return 0
}


typeset -i16 hex
typeset -u -R2 dhex2
typeset -u -R4 dhex4

weak_start_tigershark
i=0
while ((i<100)) ; do
        tigershark
        ((hex=X0))
        dhex2=0000${hex#16#}
        cum=$dhex2
        tigershark
        ((hex=X0))
        dhex2=0000${hex#16#}
        cum=${dhex2}${cum}

        tigershark
        ((hex=X0))
        dhex4=0000${hex#16#}
        cum=${dhex4}${cum}
        print -p $cum
        read -p result
        echo $result
        ((i=i+1))
done
exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Auto Ftp pseudo random file

I have a script that generates a file which is my own incarnation of a date using the 'date' function (we'll call this script a). I would like that script to invoke my other script (script b) which contains my ftp info. Since I have yet to figure out a good way to use regular ftp (yes i can only... (6 Replies)
Discussion started by: DC Slick
6 Replies

3. Shell Programming and Scripting

Replace a random string of numbers

Hi Can someone help me with this one? I have string.. (PROC_PROC_ID == 12183) <--PID is dynamic and i want to replace the PID number with whatever PID from /opt/hpws/apache32_2/logs/httpd.pid file. i'm having problem since the PID on the string is dynamic. It may be 2-5 digits or more. ... (5 Replies)
Discussion started by: ryandegreat25
5 Replies

4. Programming

Random numbers in parent/child?

Hi I'm trying to generate random numbers both in parent process and the child process. But I get the same random number in both processes. Why doesn't it generate different numbers altough I seed random number generator? Here's my code: #include <stdio.h> #include <unistd.h> #include... (2 Replies)
Discussion started by: xyzt
2 Replies

5. Shell Programming and Scripting

Generating random numbers

Hi, I am having trouble with generating random numbers. can this be done with awk? So I have a file that looks like this: 23 30 24 40 26 34 So column1 is start and column2 is end. I want to generate 3 random #'s between start and stop: So the output will look like this: ... (9 Replies)
Discussion started by: phil_heath
9 Replies

6. Shell Programming and Scripting

Random Numbers - Perl

Hi Guys I have a script to find Ranomd numbers. But I want to make the file to produce more random. Could u guys help me plz. In this Script I have the code that generates random in for loop and the range I have specified in my %chromlength input and out put will be like this chrno start end... (3 Replies)
Discussion started by: repinementer
3 Replies

7. Shell Programming and Scripting

Random numbers from 0 to 1000

Hello All, I want to make a simple script which generate random number from 0 to 1000. and simply display it. Plz HELP!!!!!! Regards, Waqas Ahmed (2 Replies)
Discussion started by: wakhan
2 Replies

8. Solaris

pseudo: [ID 129642 kern.info] pseudo-device: vol0

Hi I have a system that gave me some messages on bootup that I was not used to seeing: pseudo: pseudo-device: vol0 genunix: vol0 is /pseudo/vol@0 these came with these: Feb 13 17:42:17 system1 eri: SUNW,eri0 : 100 Mbps full duplex link up Feb 13 17:42:21 system1sendmail: My unqualified... (0 Replies)
Discussion started by: mndavies
0 Replies

9. UNIX for Dummies Questions & Answers

Random numbers without repetition

Is anyone know some scripts to generate random number without repetition using bash; for example generate 10 different random numbers. Thanks (8 Replies)
Discussion started by: asal_email
8 Replies
Login or Register to Ask a Question