MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: significant digits in random numbers in scientificform

On 2/13/04 at 2:28 AM, sean_incali at (sean kim) wrote:

>how do I pick random numbers such that, when displayed in
>ScientificForm, the coefficients only vary to designated number of

>Example: 1.2345x 10^1 has significant digits of 5 (1.2345), where
>as 1.2*10^3 has signficant digits of 2.(1.2)

>consider the following.

>In[601]:= rule1 = {k1 ->10^Random[Real,{1,3}, 2]//ScientificForm,
>k2 ->10^Random[Real,{1,3}, 5]//ScientificForm}

>k1 + k2/.rule1

>but above gives following errors.

Yes, the problem is your usage of ScientificForm. Change rule1 to be

rule1 = {k1 ->10^Random[Real,{1,3}, 2], k2 ->10^Random[Real,{1,3}, 5]}

and it will work as you expect. If you want to use ScientificForm to control the display then enclose the entire assignment above in parantheses and append //ScientificForm. Or even better IMOH, set the default output format type to TraditionalForm.


>What I wanted Mathematica to  pick was something like...

>(random real number to given digits) *10^ (random integer chosen
>from range)

>So the question is two fold. How do I tell mathematica to pick
>random numbers only in designated number of digits? and use that in
>downstream calculations?

>if I use..

>Random[Real, {1,10},5]* 10 ^ Random[Integer, {1, 3}]

>above does what I want but I hate the idea of using two Random, and
>I'm worried that it will cause problems. and i would liek to see
>how you guys would do it.

Mathematica uses two different independent algorithms for generating random reals and random integers. So, the code above should not show correlation between the random real and random integer.

However, from previous discussion of Mathematica's random number generators, I understand there are some flaws in Mathematica's implementation of the Marsagalia-Zaman subtract with borrow algorithm used to generate random reals that impact some applications. So, the corrected definition of rule1 above may have undesired correlation between the two random numbers in some cases. I don't recall the details of the flaws. But, it you check the archives of this group for discussions of random numbers you can get those details and decide whether they impact your application or not.

OTOH, as far as I know there are now issues with the Rule 30 algorithm Mathematica uses for random integers. So, I might do Random[Integer,{10000, 99999}] to get a random 5 digit number then scale it appropriately. Finally, N can be used to convert the result to a machine precision number.

>will the routine be too slow If i have to use it many many times?
>and if so why and how do i fix it?

What defines "too slow"?

Timing[Do[Random[Integer, {10000, 99999}], {10^6}]; ]

{7.14 Second,Null}

Timing[Do[Random[Real, {1, 3}, 5], {10^6}]; ]

{22.06 Second,Null}

Do these results imply Mathematica's routines are "too slow"? If so, you could try Compile or write a custom generator in C then link it to Mathematica via MathLink. But I really don't recommend writing your own random number generator unless you are really certain you know what you are doing. It is all too easy to wind up with something that is decidely not random. If you are going to create your own random number generator, you should have at least read and understood the chapters in Vol 2 of Knuth pertaining to random number generators.
To reply via email subtract one hundred and four

  • Prev by Date: Re: Problem with FullSimplify in Version 5: Rationals are converted to Reals
  • Next by Date: Re: Problem with FullSimplify in Version 5: Rationals are converted to Reals
  • Previous by thread: significant digits in random numbers in scientificform
  • Next by thread: Why does MatchQ[f[a][b],_f] return False?