Click here to Skip to main content
15,922,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have got a html with a javascript Math.random(),the source as follow:
XML
<HTML>
 <HEAD>
  <script language="javascript">
    function changecode1(obj)
    {
        obj.src="http://www.chinawestair.net/jcaptcha?.tmp="+Math.random();;
        alert(obj.src);
    }
  </script>
 </HEAD>

 <BODY>
    <table>
        <tr>
            <td width="200" height="30" align="left">
                validate code:
                <img id="jcaptcha" src="http://www.chinawestair.net/jcaptcha?.tmp=0.06781555591097987" onclick="javascript:changecode1(this)" style="cursor: pointer;height:28px;" title="change pic" border=0 />
            </td>
        </tr>
    </table>

 </BODY>
</HTML>


Now,i have write a c++ program in windows console app trying to simulate the function Math.Random().

The return value must in range [0-1] with zero in the first word,next is point,last is length of 16 number,for example "0.06781555591097987"

i need your help, give me some better advice plz.

thanks a lot.
Posted
Updated 30-May-11 0:34am
v2

I did not get the last point if you wand 16 precision(16 number after '.') use the following code,
MIDL
srand( (unsigned)time( NULL ) );
double d = (rand()/(double)RAND_MAX);
printf("%0.16lf",d);


if you want 16 digits after 0.0 use this code,

MIDL
srand( (unsigned)time( NULL ) );
double d = (rand()/(double)RAND_MAX);
printf("%0.17lf",0.1*d);
 
Share this answer
 
i think the following sentence should be make some compatibility problem...
printf("%0.17lf",0.1*d);

when i use c++,it works,but it isn't works in g++...
printf("0.16f", d);

this should be perfect i think

thank you for venkatmakam&Timberbird's warmly help.

=。=
 
Share this answer
 
Why not use rand() function?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900