shell-script-pt
[Top][All Lists]
Advanced

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

Re: [shell-script] Números aleatórios


From: Herbert Alexander Faleiros
Subject: Re: [shell-script] Números aleatórios
Date: Tue, 18 Jul 2006 12:35:32 -0300
User-agent: KMail/1.9.3

On Tuesday 18 July 2006 11:17, Eduardo Jaime Quiros Batres wrote:
> testa isto
> resp=`echo "scale=3;$(($((RANDOM%2001))-1000)) / 1000" | bc`
> echo  $resp

Olá,

A idéia é boa, mas os números estão fora da escala (-.001 < x < .001):

#!/bin/sh

for((x=0; x<100; x++)); do
#       tmp=`echo -e "4\nk\n$RANDOM\n32767\n/\n0.002\n*\n0.001\n-\np" | dc`
#       tmp=`echo "scale=4; $RANDOM / 32767 * 0.002 - 0.001" | bc`
        tmp=`echo "scale=3;$(($((RANDOM%2001))-1000)) / 1000" | bc`

        echo $tmp

        test `echo $tmp \<= .001 | bc` = 0 && echo "ERR (fora da escala)"
        test `echo $tmp \>= -.001 | bc` = 0 && echo "ERR (fora da escala)"
done


$ ./teste
[cut]
-.0360
ERR (fora da escala)
.4270
ERR (fora da escala)
.1380
ERR (fora da escala)
-.1520
ERR (fora da escala)
[cut]


Sobre as verificações efetuadas (determinar se está dentro da escala):

$ man bc
[cut]
        Relational  expressions  are  a special kind of expression that always
       evaluate to 0 or 1, 0 if the relation is false and 1 if the relation is
       true.
[cut]
       expr1 == expr2
              The result is 1 if expr1 is equal to expr2.
[cut]


Até,

Herbert.

PS - faça o mesmo teste com a minha implementação dc/bc.


reply via email to

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