1
$\begingroup$

Let's suppose that we want to use random guessing to find a matrix G that satisfies the relation F=G.{x,y}:

F={x+y}
Do[If[F == RandomInteger[{-1, 1}, {1, 2}].{x, y}, 
  Print[RandomInteger[{-1, 1}, {1, 2}]]], 100]

Is there any way to print the random matrix that satisfies the If-condition inside a Do-loop, because every next call of RandomInteger changes the matrix? Sorry for my type style, first time here.

$\endgroup$
1
$\begingroup$

You should generate random pair once per each loop. E.g.

F = {x + y};
Do[If[F == # . {x, y}, Print[#]] &[
  RandomInteger[{-1, 1}, {1, 2}]], 100]

or, in a more traditional way:

F = {x + y};
Do[pair = RandomInteger[{-1, 1}, {1, 2}]; 
 If[F == pair . {x, y}, Print[pair]], 100]
$\endgroup$

Your Answer

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

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