Skip to main content
added 504 characters in body
Source Link
Andy Ross
  • 19.5k
  • 2
  • 62
  • 93

This is wasteful but, if I understand the problem correctly, it does the trick with minimal code using ReplaceRepeated.

n = 10000; limitValue = 0.5; RandomReal[{0, 1}, n] //. x_ /; x < limitValue :> x + RandomReal[{0, 1}] 

I was actually surprised at the speed of it.

Edit:

Per request in the comments. To keep track of the number of additions we can modify things slightly with Reap and Sow.

n = 10000; limitValue = 0.5; {res, count} = Reap[Transpose[{RandomReal[{0, 1}, n], Range[n]}] //. {x_, i_} /; x < limitValue :> (Sow[i]; {x + RandomReal[{0, 1}], i})]; 

Now res[[All,1]] contains the values you want and Tally[count[[1]]] contains a list of pairs {i,k} where i is the index and k is the number of additions.

This is wasteful but, if I understand the problem correctly, it does the trick with minimal code using ReplaceRepeated.

n = 10000; limitValue = 0.5; RandomReal[{0, 1}, n] //. x_ /; x < limitValue :> x + RandomReal[{0, 1}] 

I was actually surprised at the speed of it.

This is wasteful but, if I understand the problem correctly, it does the trick with minimal code using ReplaceRepeated.

n = 10000; limitValue = 0.5; RandomReal[{0, 1}, n] //. x_ /; x < limitValue :> x + RandomReal[{0, 1}] 

I was actually surprised at the speed of it.

Edit:

Per request in the comments. To keep track of the number of additions we can modify things slightly with Reap and Sow.

n = 10000; limitValue = 0.5; {res, count} = Reap[Transpose[{RandomReal[{0, 1}, n], Range[n]}] //. {x_, i_} /; x < limitValue :> (Sow[i]; {x + RandomReal[{0, 1}], i})]; 

Now res[[All,1]] contains the values you want and Tally[count[[1]]] contains a list of pairs {i,k} where i is the index and k is the number of additions.

Post Undeleted by Andy Ross
Post Deleted by Andy Ross
Source Link
Andy Ross
  • 19.5k
  • 2
  • 62
  • 93

This is wasteful but, if I understand the problem correctly, it does the trick with minimal code using ReplaceRepeated.

n = 10000; limitValue = 0.5; RandomReal[{0, 1}, n] //. x_ /; x < limitValue :> x + RandomReal[{0, 1}] 

I was actually surprised at the speed of it.