Jump to content


Photo
- - - - -

Random Number Generator


8 replies to this topic

#1 jloflin

jloflin

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 16 November 2004 - 08:18 PM

I'm working with version 5.5 native pro-iv on a unix box. I'm needing to generate random numbers. Is there a way within pro4 that this could be done? Any help would be appreciated.

Thanks,
Joel

#2 Joseph Bove

Joseph Bove

    ProIV Guru

  • Members
  • PipPipPipPipPip
  • 756 posts
  • Gender:Male
  • Location:Ramsey, United States

Posted 16 November 2004 - 09:02 PM

Joel,

Here's an ugly answer. Hopefully someone has something better for you.

Call an external awk script

# rand.awk -- test random number generation
BEGIN {
srand()
print rand()
}

You can then set the output to a system variable or place it in a file and retrieve the value.

hth,

Joseph

#3 jloflin

jloflin

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 16 November 2004 - 09:30 PM

Thanks for the help. I'll try that out and see how it goes. I was truly hoping there was some pro4 command that I was missing.

Thanks again for the advice!

Joel

#4 Guest_George_*

Guest_George_*
  • Guests

Posted 17 November 2004 - 06:01 PM

Here's a solution using ProIV code:

PRECISION(20)
#Numb1 = 2 ** 50
#Numb2 = INT((#Numb1 - 1) * FRAC(CTIME(@TIME) + (@DATE / 1000000000000)))
#Numb3 = #Numb1 - 1 - #Numb2
#Numb3 = INT(REM((#Numb2 * #Numb3),#Numb1))
#RANDOM = (#Numb3 / #Numb1) * 10000

#5 Joseph Bove

Joseph Bove

    ProIV Guru

  • Members
  • PipPipPipPipPip
  • 756 posts
  • Gender:Male
  • Location:Ramsey, United States

Posted 17 November 2004 - 07:15 PM

George,

Does that solution only produce one random number per second?

Regards,

Joseph

#6 hath

hath

    Member

  • Members
  • PipPip
  • 17 posts
  • Gender:Male

Posted 17 November 2004 - 11:22 PM

Alternatively, you could use Oracle utility: DBMS_RANDOM.RANDOM function to generate a random number for you. Of course, your application must have embbeded oracle database.

#7 Joseph Bove

Joseph Bove

    ProIV Guru

  • Members
  • PipPipPipPipPip
  • 756 posts
  • Gender:Male
  • Location:Ramsey, United States

Posted 17 November 2004 - 11:50 PM

With great respect to Douglas Adams (Hitchhiker's Guide, etc.) you code hard code your random number as 42.

Just out of curiousity, why would you want a random number in a ProIV application?

Regards,

Joseph

#8 Rick Young

Rick Young

    ProIV Guru

  • Members
  • PipPipPipPipPip
  • 265 posts
  • Gender:Male
  • Location:Guelph, Canada

Posted 18 November 2004 - 04:24 PM

Although I liked the idea of returning the answer to the question of life, the universe, and everything....I decided that this seemed like a good way to wake up properly :rolleyes:

I don't pretend that it's elegant....

N PARMS(#MULT)
PRECISION(20)
IF #MULT = 0 #MULT = 100;
$TERM = @TERM
#LEN = 1
WHILE #LEN > 0
#LEN = LEN($TERM)
IF $TERM(1,#LEN) IN-RANGE '0','9'
$TERM2 = $TERM2 + CONV(CONV($TERM(1,#LEN)) * 2 ** (#LEN - 1))
$TERM = $TERM(2,#LEN)
ELSE
$TERM2 = $TERM2 + CONV(ORD($TERM(1,#LEN)) * 2 ** (#LEN -1))
$TERM = $TERM(2,#LEN)
ENDIF
ENDWHILE
#NUM1 = CONV($TERM2)
#DD = CDATE(CDATE(@DATE,'DD'))
IF REM(#DD,2) = 0
#NUM1 *= @DATE
ELSE
#NUM1 /= @DATE
ENDIF
IF REM(CTIME(@TIME),2) = 0
#NUM1 /= CTIME(@TIME)
ELSE
#NUM1 *= CTIME(@TIME)
ENDIF
#NUM1 /= 1000000000000000
#RANDOM = ROUND((FRAC(#NUM1) * #MULT),0)
RETURN(#RANDOM)

Where this glogic would be called like #I = RAND_N(#MULT)

#9 jloflin

jloflin

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 18 November 2004 - 07:14 PM

Thanks for all of the help. The reason I'm needing a random number is that for audit purposes we have to do random samplings of different charts. I work at a hospital and regulations are forcing us to do this.

Thanks again for the help!

Joel



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users