ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Random Selection

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Random Selection

    Hi All,

    Need some ideas here. I am creating a customer selection routine as follows:

    1) top 20 customer
    2) all new customers
    3) randomly select 10 customers not fitting 1) & 2)

    need advice on 3). There may not always be 10 left to select.

    Not sure on how to select the randoms and still be random. Keep in mind I don't wanr to use up the randoms too soon in the process. Then that wouldn't be very random.

    a) some customer information compared to millisecond (portion of zip code)
    b) the millisecond looked up on a alpha table if the customer's name begins
    c) customer's contact bust size ......who knows

    open to programmical random selections (can random really be programmed?)

    Thnx
    Bill
    Bill
    "A good friend will bail you out of jail,
    A true friend would be sitting beside you saying,
    'Wow, that was fun.'"

  • #2
    Re: Random Selection

    How is this?

    Code:
    X = total number of customers/10   
    
    Do 10 times
      Y =   [random(X) records *2 ]   
      Do Y Times
         Read CustFile
      EndDo
      If customer is not already selected
         Write to selection Workfile
      EndIf
    EndDo
    This should give a somewhat random selection of customers assuming your file sort order is not on something important like credit rating or RMA frequency.


    Use CEERAN0 to generate random intervals
    Last edited by itp; January 8, 2007, 06:10 PM.

    Comment


    • #3
      Re: Random Selection

      --or-- here


      SQL
      PHP Code:
          ˆH Debug NoMain

            // *----------------------------------------------------------------*
            // *‚                   Precision Sytems, Inc.                    € *
            // *----------------------------------------------------------------*
            // *                                                                *
            // *  System name. . :‚ *Any                                      € *
            // *  Module/Program :‚ GETRANDNUM                                € *
            // *  Text . . . . . :‚ Generate a "Random" Number                € *
            // *  Author . . . . :‚ Randy Weber                               € *
            // *                  ‚ PrecisionSystems@comcast.net              € *
            // *                                                                *
            // *  Remarks  . . . :  This module uses SQL to retrieve a          *
            // *                    random number using the Microseconds        *
            // *                    portion of the current timestamp to         *
            // *                    seed the SQL RAND routine.                  *
            // *                                                                *
            // *                  €„Parameters:€                                *
            // *                    1. Random - Packed 9,8                      *
            // *                                                                *
            // *                  €„Sample Create Command€                      *
            // *                                                                *
            // *                    CRTSQLRPGI                                  *
            // *                        OBJ(MyLib/GETRANDNUM)                   *
            // *                         SRCFILE(MyLib/QRPGLESRC)               *
            // *                         SRCMBR(GETRANDNUM)                     *
            // *                         COMMIT(*NONE)                          *
            // *                         OBJTYPE(*MODULE)                       *
            // *                         REPLACE(*YES)                          *
            // *                         DBGVIEW(*SOURCE)                       *
            // *                                                                *
            // *                  €„Sample GETRANDNUM Usage€                    *
            // *                                                                *
            // *                     // ‚Random number interface€               *
            // *                    D GetRandNum      PR             9P 8       *
            // *                                                                *
            // *                    D Random          S              9P 8       *
            // *                                                                *
            // *                     // ‚Free-form calc specs€                  *
            // *                             Random = GetRandNum() ;            *
            // *                                                                *
            // *                                                                *
            // *----------------------------------------------------------------*

            //‚Module interface
           
      D GetRandNum      PR             9P 8


          
      ˆ /Title  Use SQL to Get a Random NumberŠ...€
            // ‚***********************************************************
           
      P GetRandNum      B                   Export
            
      // ‚***********************************************************

           
      D GetRandNum      PI             9P 8

            
      // ‚Stand Alone Fields
           
      D Random          S              9P 8

           C                   
      Eval      Random = *Zeros

             
      // ‚Run the SQL statement
           
      C/EXEC SQL
           C
      +
           
      C+   SELECT Dec(
           
      C+              Rand(
           
      C+                   Int(
           
      C+                       Substr(
           
      C+                              Char(Current_TimeStamp),21,6
           C
      +                             )
           
      C+                       )
           
      C+                  ),9,8
           C
      +             ) Into :Random
           C
      +     FROM QsqpTabl With NC
           C
      +
           
      C/END-EXEC

           C                   
      Eval      Random = %Abs(Random)
           
      C                   Return    Random

           P GetRandNum      E


         
      ˆˆ /Title  Generate a "Random" NumberŠ...€ 
      UUID

      PHP Code:
      *

           
      H Option( *NoSrcStmt )  BNDDIR('QC2LE')
           **
           
      D UUID_template   Ds
           D  UtBytPrv                     10u 0 Inz
      ( %SizeUUID_template ))
           
      D  UtBytAvl                     10u 0
           D                                8a   Inz
      ( *Allx'00' )
           
      D  UUID                         16a
           D  Reply                         1a
           
      **
           
      D GenUuid         PR                  ExtProc('_GENUUID')
           
      D  UUID_template                  *   Value
           
      **
           
      D ToHex           PR                  EXTPROC('cvthc')
           
      D  szHexVal                  65534A   OPTIONS(*VARSIZE)
           
      D  szCharVal                 32767A   Const OPTIONS(*VARSIZE)
           
      D  nSrcLen                      10I 0 VALUE
           
      **
           
      D pMemberName     s             10
           D  OutField       s          65534
           D  shorter        s             16
           c                   
      do        5
           C                   Callp     GenUuid
      ( %AddrUUID_template ))
           
      C                   Callp     ToHex(OutField UUID :16)
           
      c                   eval      shorter = %subst(outfield:1:16)
           
      c     shorter       dsply                   Reply
           c                   enddo
           
      **
           
      C                   Eval      *INLR = *on 
      All my answers were extracted from the "Big Dummy's Guide to the As400"
      and I take no responsibility for any of them.

      www.code400.com

      Comment

      Working...
      X