0
$\begingroup$

This question already has an answer here:

I generated a 2d random array in $x-y$ plane with

L = 10;
random = Table[{x, y, RandomReal[{-1, 1}]}, {x, 0, L, L/10}, {y, 0, L, L/10}];

Now I want to save it for the next using by

iniF = Interpolation[Flatten[random, 1]];
inif[x_, y_] = c+iniF[x, y];

where $c$ is constant. How do you save the random data with a convenient format? Thank you!

$\endgroup$

marked as duplicate by MarcoB, user9660, Yves Klett, ubpdqn, Öskå Jun 29 '16 at 16:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ what do you want to save? Isn't the array already saved in random? $\endgroup$ – happy fish Jun 27 '16 at 5:35
  • $\begingroup$ @ happy fish, Yes, it is in random of the memory. But after I close the notebook, or ClearAll, it is lost and I need to re-generate a random array. Here, I want to save random in a certain kind of data file in my disk for the next access. Thanks! $\endgroup$ – jsxs Jun 27 '16 at 7:08
  • $\begingroup$ Export $\endgroup$ – Sektor Jun 27 '16 at 7:11
  • $\begingroup$ "But after I close the notebook, or ClearAll, it is lost and I need to re-generate a random array." No, closing the notebook won't clear the random, it's still there. $\endgroup$ – xzczd Jun 27 '16 at 7:23
  • 4
    $\begingroup$ @xzczd true, but the OP may mean closing the kernel session. This may be useful (search for "persistent"): Making large calculation results persistent $\endgroup$ – Yves Klett Jun 27 '16 at 7:32
4
$\begingroup$

As a general solution, you can always Export you data to a file.

L = 10;
random = Table[{x, y, RandomReal[{-1, 1}]}, {x, 0, L, L/10}, {y, 0, L, L/10}];

Export[NotebookDirectory[]<>"rand.dat",Flatten[random,1]]

Next time you want to use this data

random = Import[NotebookDirectory[]<>"rand.dat"];

One benefit of using data file is you can use the data with any other language as well and also share with others. If you are working only with MMA, using a Seed as suggested by Wjx would be a better option.

$\endgroup$
  • $\begingroup$ Yes, that's the way to actually "save" it~ $\endgroup$ – Wjx Jun 27 '16 at 10:06
6
$\begingroup$

Add this simple code before u run everything will let Random generate exactly the same result every time you run. If you're careful enough, you may find this in lot's of posts with randomly generated input.

SeedRandom["Whatever you write here, keep it the same in multiple runs!"]

Hope this can help you!

$\endgroup$

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