Message Boards Message Boards

How to generate a list from a loop?

Posted 10 years ago
POSTED BY: Greg
3 Replies
POSTED BY: Carl Verdon

one way

First@Last@Reap@Do[Do[If[j*RandomReal[] > 1/2, Sow@{i, i^2, i^3}; Break[]], {j, 1, 9, 1}], {i, 1, 3, 1}]
   (*{{1, 1, 1}, {2, 4, 8}, {3, 9, 27}}*)
POSTED BY: Nasser M. Abbasi
Posted 10 years ago

That works very nicely. It looks to me like Reap/Sow is a great way when table won't work (as it doesn't for me in this instance). Concise, and functional, thanks!

POSTED BY: Greg
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract