Random numbers from 1 to 20

How do I generate a random number from 1 to 20 in natural?

I do not know how to do it?

Searching this site for Random Number turns up this article:

https://tech.forums.softwareag.com/t/prim-numbers-and-random-values/61298

There are several options to choose from.

1 Like

Various methods. I like taking the timestamp and in your case dividing by 20 and take the remainder as your random number…

Eugene (Gene) Miklovich

ADABAS/NATURAL Systems Support and DBA

Cell: 916-202-7047

Normal Hours: 8am-4pm E.T.

Please call or text if assistance needed outside these hours.

Out of Office Thursday Mar 9th-Sat Mar 11th.

1 Like

In our code samples in GitHub you may also find some ideas.

adabas-natural-code-samples/Prim numbers and random values

1 Like

thanks, this link had a program that I adapted and it worked

 Top    ....+....1....+....2....+....3....+....4....+....5....+....6....+....7..
   0010 * -------------------------------------------------------------------- *
   0020 * FUNCAO - GERAR NUMERO ALEATORIO                                      *
   0030 * -------------------------------------------------------------------- *
   0040 DEFINE DATA LOCAL                                                       
   0050 1 #RAND-F8   (F8)                                                       
   0060 1 #PRIM      (I4) INIT <2147483629>                                     
   0070 1 #P1        (I4)  /* P-1                                               
   0080 1 #PH        (I4)  /* (P-1)/2                                           
   0090 1 #VAL       (I4)                                                       
   0100 1 #VAL1      (I4)                                                       
   0110 1 #STCL      (B8)                                                       
   0120 1 REDEFINE #STCL                                                        
   0130   2 #S1      (B2)                                                       
   0140   2 #T4      (I4)  /* RANDOM START IF REQUIRED                          
   0150   2 REDEFINE #T4                                                        
   0160     3 #T4A   (B3)                                                       
   0170     3 #T1    (B1)  /* 0 - 255 RANDOM REPEAT                             
   0180   2 #S2      (B2)                                                       
   0190 1 #R2        (I2)                                                       
   0200 1 REDEFINE   #R2                                                        
   0210   2 #R21     (B1)                                                       
   0220   2 #R22     (B1)                                                       
   0230 1 #I         (I4)                                                       
   0240 1 #J         (N4)                                                       
   0250 1 #F         (F8)                                                       
   0260 INDEPENDENT                                                             
   0270 1 +RAND      (I4)                                                       
   0280 END-DEFINE                                                              
   0290 * -------------------------------------------------------------------- *
   0300 #P1 := #PRIM - 1                                                        
   0310 #PH := #P1 / 2                                                          
   0320 MOVE *TIMESTMP TO #STCL                                                 
   0330 IF +RAND = 0  /* FIRST CALL                                             
   0340   MOVE #T4 TO #VAL  /* RANDOM TIME                                      
   0350   IF #VAL >= #P1  /* MOD P-1                                            
   0360     #VAL := #VAL - #P1                                                  
   0370   ELSE                                                                  
   0380     REPEAT                                                              
   0390       IF #VAL >= 0                                                      
   0400         ESCAPE BOTTOM
   0410       ELSE                                                              
   0420         #VAL := #VAL + #P1                                              
   0430       END-IF                                                            
   0440     END-REPEAT                                                          
   0450   END-IF                                                                
   0460   +RAND := #VAL + 1   /* RANGE 1 TO P-1                                 
   0470 END-IF                                                                  
   0480 #VAL := +RAND                                                           
   0490 WRITE +RAND                                                             
   0500 * -------------------------------------------------------------------- *
   0510 RESET #R22                                                              
   0520 MOVE #T1 TO #R2   /* GET UNSIGNED (I1)                                  
   0530 FOR #I = 1 TO #R2 /* ** RAND(I1)                                        
   0540   IF #VAL > #PH  /*   2 * #VAL > P                                      
   0550     #VAL1 := #PRIM - #VAL /* AVOID OVERFLOW                             
   0560     #VAL := #VAL - #VAL1 /* = V-(P-V) = 2V-P = 2V (MOD P)               
   0570   ELSE                                                                  
   0580     ADD #VAL TO #VAL                                                    
   0590   END-IF                                                                
   0600 END-FOR
   0610 MOVE #VAL TO +RAND                                                      
   0620 #F := #VAL - 1                                                          
   0630 #RAND-F8 := #F / #P1                                                    
   0640 * -------------------------------------------------------------------- *
   0650 FETCH 'CSA@A006'                                                        
   0660 END                                                                     
        ....+....1....+....2....+....3....+....4....+....5....+... S 66   L 47

Result 1

 PAG      1                                                   19/04/23  12:55:17
                                                                                
   567229186

Result 2

 PAG      1                                                   19/04/23  12:55:28
                                                                                
   478117814

Result 3

 PAG      1                                                   19/04/23  12:55:34
                                                                               
  1716853068

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.