# How to generate two random points when a button is pressed

I want to plot two random points when a user presses a button. I have done some work but nothing happens when the button is pressed. The points remain at their current locations.

My code is as follows:

Manipulate[
DynamicModule[{pts = {{5, 1}, {3, 2}}},
{
Graphics[{
Purple, PointSize[0.02], Point[Dynamic[pts[[1]]]],
Black, PointSize[0.02], Point[Dynamic[pts[[2]]]],
If[visualCheck == True, {Black, Circle[CenterPoint, radius]}, {}
]
},
Axes -> True, PlotRange -> {{-10, 10}, {-10, 10}},
ImageSize -> Large]
}
](*DynamicModule*),

(*Controllers*)
Button["Click", pts = RandomSample[Tuples[Range[-10, 10], 2], 2]],
{{CenterPoint, {1, 1}}, Locator},
{{visualCheck, False, Style["Visual Check", 12, Black]}, {True,
False}, Checkbox}

]


*I want to change the Black and Purple points when the button is pressed.

Screenshot:

• why are using DynamicModule inside Manipulate for?? Manipulate itself is DynamicModule You making things way too complicated. If you want I can show you how to I want to plot two random points when the user press a button but I can not follow your code as is. I have to write new one without dynamicmodule inside manipulate. Sep 23, 2022 at 10:54
• I'm "new" to mathematica, I did not know that Manipulate itself is a DynamicModule thanks for letting me know. if you can show me how you would do it, it will be great Sep 23, 2022 at 11:03

Here is basic implementation. My advice, always start simple and when it is working, you can add more features.

Manipulate[
Module[{pt1 = pt[[1]], pt2 = pt[[2]]},
Grid[{
{pt},
{Graphics[{{Red,PointSize[0.05],Point[pt1]}, {Blue,PointSize[0.05], Point[pt2]}},
PlotRange -> {{-10, 10}, {-10, 10}},
Axes -> True,
GridLines -> Automatic, GridLinesStyle -> LightGray]}}
]
]
,
Grid[{{Button["Click", pt = RandomSample[Tuples[Range[-10, 10], 2], 2]]}}],
{{pt, {{0, 0}, {0, 0}}}, None},
TrackedSymbols :> {pt}
]

• Thank you, this helps a lot! Sep 23, 2022 at 11:27