I have defined the following function:

randvec[mag_]:= mag Normalize[Table[RandomReal[], {j, 1, 4}]]

I would like to format the numbers in the list so that they are 4 d.p. Trying the following:

randvec[mag_] := NumberForm[mag Normalize[Table[RandomReal[], {j, 1, modes}]], 4]

casts each entry as a string. What is the appropriate way to format the list without changing the type of the entries?

  • $\begingroup$ randvec[mag_] := N@Round[mag Normalize[Table[RandomReal[], {j, 1, 4}]], 1*^-4]? $\endgroup$ – MarcoB Jan 14 at 18:30
  • $\begingroup$ A couple more points. RandomReal[{0, 1}, modes] will do the same as your table, but more efficiently. I also wonder if this question dealing with the generation of random vectors with a specified magnitude might be relevant to you: Vectors with a certain magnitude in Mathematica. $\endgroup$ – MarcoB Jan 14 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.