# Why do I keep getting 0.06 with WorkingPrecision->1?

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?

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.

• Thies, that last method has the same problem as the OP's method; take a look at it. Jun 19, 2012 at 17:42
• What about : N[Rationalize[RandomReal[UniformDistribution[], 1000], 0.1], 1] ?
– Öskå
Jun 19, 2012 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. Jun 19, 2012 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? Jun 19, 2012 at 22:10
• sure, thanks for the binary representation hint! Jun 19, 2012 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}
• 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, 2012 at 22:30