gnucobol-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [open-cobol-list] RANDOM-ly non-random


From: address@hidden
Subject: Re: [open-cobol-list] RANDOM-ly non-random
Date: Fri, 31 May 2013 07:35:58 -0400

Yes, this "check every other against ten" was the intention of the original author. The idea was to generate a ten sometimes.

----- Reply message -----
From: "Michael Anderson" <address@hidden>
To: <address@hidden>
Subject: [open-cobol-list] RANDOM-ly non-random
Date: Fri, May 31, 2013 5:56 am


After looking a little more, it seems that you are performing "Generate-And-display-Num" twice in the perform loop, but only testing for ten once.

Could be wrong, but should your logic look more like:
 IDENTIFICATION DIVISION.
 PROGRAM-ID. randloop.
 *> cobc -x -free -ffunctions-all randloop.cob
 DATA DIVISION.
 WORKING-STORAGE SECTION.
 1    Num  PIC Z9.
 1    SEED PIC 9V999999999.
PROCEDURE DIVISION.

 Main.
    MOVE RANDOM(SECONDS-PAST-MIDNIGHT) TO SEED.
    PERFORM FOREVER
        PERFORM Generate-And-Display-Num
        IF Num = 10
            EXIT PERFORM
        END-IF
    END-PERFORM
    GOBACK.


 Generate-And-Display-Num.
   COMPUTE Num =  REM(RANDOM * 100, 20).
   DISPLAY Num.






On 05/30/2013 09:38 PM, Hiroki MINEMATSU wrote:
Hello, Bruce

OpenCOBOL RANDOM returned biased value.

It is look like conacatinate '0.' and int(rand()*(2**32)) .

so, you execute this.

perl -e '$a = 0; for (1..1000) {$p = int(rand() * (2 ** 31)) ;$a +=
"0.".$p; }; print $a ;print "\n";'

it will show about 347 instead of 500.

Since it is wrong, we have to correct libcob.so.


(2013/05/30 23:30), Bruce M. Axtens wrote:
Context: MinGW

    $ uname -a
    MINGW32_NT-6.1 MERCURY 1.0.17(0.48/3/2) 2011-04-24 23:39 i686 Msys

    $ cobc -v
    cobc (OpenCOBOL) 1.1.0
    Copyright (C) 2001-2009 Keisuke Nishida / Roger While
    Built    May 29 2013 21:53:19
    Packaged Feb 06 2009 10:30:55 CET


Sadly, I don't get to do much COBOL. The best I can do is the occasional
RosettaCode task.

I was looking at <http://rosettacode.org/wiki/Loops/Break#COBOL>. I
copied and pasted into a text editor, made it free-form, and started to
fiddle.

I've got as far as this:

    IDENTIFICATION DIVISION.
    PROGRAM-ID. Random-Nums.

    DATA DIVISION.
    WORKING-STORAGE SECTION.
    01    Num  PIC Z9.
    01    SEED PIC 9V999999999.
    PROCEDURE DIVISION.
    Main.
        MOVE FUNCTION RANDOM(FUNCTION SECONDS-PAST-MIDNIGHT) TO SEED.
        PERFORM FOREVER
            PERFORM Generate-And-Display-Num

            IF Num = 10
                EXIT PERFORM
            ELSE
                PERFORM Generate-And-Display-Num
            END-IF
        END-PERFORM

        GOBACK
        .

    Generate-And-Display-Num.
       COMPUTE Num =  FUNCTION REM(FUNCTION RANDOM * 100, 20)
       DISPLAY Num
       .
       END-PROGRAM.

The END-PROGRAM seemed to be mandatory. I wouldn't compile otherwise.

The thing that has me confused (and thus all the fiddling with RANDOM)
is that the program runs properly occasionally, and other time just gets
locked on a particular value and doesn't change.

A working runs looks like this:

    $ breakloop.exe
    23
    67
    23
    21
    67
    18
    14
    17
    27
    11
    16
    25
    29
    26
    12
    23
    90
    27
    14
    20
    30
    29
    10


but another run starts off nicely, but then gets stuck

    $ breakloop.exe
    25
    18
    29
    80
    14
    16
    26
    24
    26
    36
    18
    29
    54
    47
    29
    29
    29
    ... ad infinitum

Why is it so?

Kind regards,
Bruce.



------------------------------------------------------------------------------
Introducing AppDynamics Lite, a free troubleshooting tool for Java/.NET
Get 100% visibility into your production application - at no cost.
Code-level diagnostics for performance bottlenecks with <2% overhead
Download for free and get started troubleshooting in minutes.
http://p.sf.net/sfu/appdyn_d2d_ap1



_______________________________________________
open-cobol-list mailing list
address@hidden
https://lists.sourceforge.net/lists/listinfo/open-cobol-list




reply via email to

[Prev in Thread] Current Thread [Next in Thread]