OpenEdge Random Number Generator

Posted by rrajan on 29-Jan-2013 08:34

Do we have any function which would randomly generate a number between a range such as 1 & 100. The Random function gives a random number between 1 & 100 but the problem is, it repeats the same value again so it doesn't always return a new value between 1 & 100.

All Replies

Posted by Peter Judge on 29-Jan-2013 08:41

The RANDOM() function can take a range. Also look at the -rand startup parameter for details on how to control the number reuse.

-- peter

Posted by rrajan on 29-Jan-2013 08:50

I checked the RANDOM function and the -rand startup parameter but it seems there is no way by which we can prevent the same number selected again randomly in the same session using either of the random number generators.

Posted by robw@hltool.com on 29-Jan-2013 09:01

roll your own then..

Posted by robw@hltool.com on 29-Jan-2013 09:06

define var number# as integer no-undo.

define var ok# as logical no-undo,

define temp-table ttrandom

    field randnumber as integer 

    index ttindex as primary unique

    randnumber.

empty temp-table ttrandom.

ASSIGN

     Number# = random (whatever you want).

     ok# = false.

find first ttrandom where

    Ttrandom.randnumber = number#

    no-lock no-error.

if not avail ttrandom then

do:

    create ttrandom.

    Ttrandom.randnumber = number#.

    ok# = true. 

    <>

end.

use this for idea not verbatim - change the while not ok# loop to suit your needs.

Posted by gus on 29-Jan-2013 11:16

if you specify the -rand 2 client startup parameter, then a different random number generator is used. This 32-bit linear congruential generator is seeded with a different value for each session. The default random number generator is seeded with the same value each session by design.

Posted by gus on 29-Jan-2013 11:18

This is not a good solution.

If you need to implement a random number generator, go find some suitable proven algorithm and use that. It is quite difficult to implement a good random number generator and trial and error will never get you there.

Posted by robw@hltool.com on 29-Jan-2013 11:31

If he wants to guarantee uniqueness, then yes this will work.

If he wants a better algorithm, then yes find a nifty snazzy algorithm.

Posted by rrajan on 30-Jan-2013 03:51

I tried using -rand 2, still the problem I am facing is the same number is generated twice or more than that in the same session ideally I need to generate a random number which doesn't repeat in the same session.

Posted by jmls on 30-Jan-2013 04:26

I suspect that you should be looking at guid(generate-uuid) rather than random

On 30 January 2013 09:51, Ramalingam Veerakumararajan

Posted by rrajan on 30-Jan-2013 07:16

I need the random number between a lower and an upper limit number

Posted by gus on 30-Jan-2013 13:56

All right then. Here is some code that gives you the numbers from 1 to 100 in random order.

def var i as int no-undo.

def var r as int no-undo.

def var t as int no-undo.

def var n as int extent 100 no-undo.

/* make a list of 100 numbers */

do i = 1 to 100:

    n[i] = i.

end.

/* scramble the list */

do i = 100 to 2 by -1:

    r = random (1, i).

    if (r = i) then next.

    t = n[i].

    n[i] = n[r].

    n[r] = t.

end.

/* do something with the numbers */

do i = 1 to 100:

    . . . .

end.

Posted by Marko Myllymäki on 31-Jan-2013 07:34

This should work in 10.2A+:

DEF VAR oRandom AS System.Random.

oRandom = NEW System.Random().
MESSAGE oRandom:Next(1,100).

DELETE OBJECT oRandom.

Edit: sorry, I didn't read carefully, you wanted to prevent using the same value twice. I guess this won't work any better that RANDOM function.

Posted by jquerijero on 31-Jan-2013 17:52

I think he mean that two different runs of his application produce the same series of random numbers.

Based on the documentation of System.Random() it uses the system clock as the seed, so it should give different series of random numbers each application run.

Posted by gus on 01-Feb-2013 09:07

Let's start over. I will try to be precise in restating the problem.

As I understand it, what you need is a series of up to 100 number values such that:

  • each number is an integer in the range 1 to 100 inclusive
  • each number in the series occurs only once
  • the numbers are in a "random" order

is that correct? Are there any other requirements ?

Posted by rrajan on 04-Feb-2013 01:25

Yes Gus this is exactly what I need, but the end can be any number "n".

Posted by gus on 04-Feb-2013 08:44


Then the code i posted earlier should be sufficient.

This thread is closed