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.
The RANDOM() function can take a range. Also look at the -rand startup parameter for details on how to control the number reuse.
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.
roll your own then..
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
empty temp-table ttrandom.
Number# = random (whatever you want).
ok# = false.
find first ttrandom where
Ttrandom.randnumber = number#
if not avail ttrandom then
Ttrandom.randnumber = number#.
ok# = true.
use this for idea not verbatim - change the while not ok# loop to suit your needs.
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.
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.
If he wants to guarantee uniqueness, then yes this will work.
If he wants a better algorithm, then yes find a nifty snazzy algorithm.
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.
I suspect that you should be looking at guid(generate-uuid) rather than random
On 30 January 2013 09:51, Ramalingam Veerakumararajan
I need the random number between a lower and an upper limit number
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.
/* 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.
/* do something with the numbers */
do i = 1 to 100:
. . . .
This should work in 10.2A+:
DEF VAR oRandom AS System.Random.
oRandom = NEW System.Random().
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.
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.
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:
is that correct? Are there any other requirements ?
Yes Gus this is exactly what I need, but the end can be any number "n".
Then the code i posted earlier should be sufficient.