Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to generate a list of 1000 random numbers with WorkingPrecision->1. Is there any particular reason that I'm getting some entries as 0.06?

I have tested the following lines:

X=RandomReal[UniformDistribution[],1000,WorkingPrecision->1]

or

X=RandomVariate[UniformDistribution[],1000,WorkingPrecision->1]

Is there a way to omit 0.06 and get true 1 precision, or am I missing something?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

WorkingPrecision is really more meant for setting the internal number precision while calculating your result. If you want a random number with one floating point digit you're probably better off with

Round[#, 0.1] & /@ RandomVariate[UniformDistribution[], 1000]

or

1/10 RandomInteger[{0, 10}, 1000] // N

or you can also set the precision or accuracy explicitly

SetPrecision[#, 1] & /@ RandomReal[{0,1}, 1000]
SetAccuracy[#, 2] &  /@ RandomReal[{0,1}, 1000]

Edit: Like Mr.Wizard says, SetPrecision and SetAccuracy have a similar problem than your original solution, even though they are changing the precision after the calculation, because they have to decide how many binary digits to grant to the internal representation of the number. That's why this won't give a satisfying decimal representation in all cases. The other methods are safer in that regard.

share|improve this answer
    
Thies, that last method has the same problem as the OP's method; take a look at it. –  Mr.Wizard Jun 19 '12 at 17:42
    
What about : N[Rationalize[RandomReal[UniformDistribution[], 1000], 0.1], 1] ? –  Öskå Jun 19 '12 at 17:48
    
@Mr.Wizard: yeah, now i see, that it's not optimal. SetAccuracy[#, 2] & is a bit better but still suffers from the binary representation. –  Thies Heidecke Jun 19 '12 at 22:05
    
I voted for your post because it answers the practical. If you feel that my assertion about binary representation is correct, will you vote for mine? Or suggest how I may improve it? –  Mr.Wizard Jun 19 '12 at 22:10
    
sure, thanks for the binary representation hint! –  Thies Heidecke Jun 19 '12 at 22:17

I believe your problem is the result of the numbers' internal binary representation.

Consider this:

Tuples[{0, 1}, 3]

FromDigits[{#, -1}, 2] & /@ %

SetPrecision[%, 1]
{{0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1}}

{0, 1/16, 1/8, 3/16, 1/4, 5/16, 3/8, 7/16}

{0, 0.06, 0.1, 0.2, 0.3, 0.3, 0.4, 0.4}
share|improve this answer
1  
Yep, I was also trying to understand where the 0.06 came from, and if you try InputForm /@ Union[RandomReal[UniformDistribution[], 1000, WorkingPrecision -> 1]] you will see the actual real numbers you get, and all are fractions of 16. –  FJRA Jun 19 '12 at 22:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.