How to choose a random atom in the region (not all)?


I am trying to select a random atom in the region or group (not all), but it seems that there is no built-in function in lammps. Is there any alternative way to do it?


The first question I would ask is why do you want to “select” a single atom? What insights would you gain from inspecting a random one that you couldn’t from an ensemble?

Second, what do you mean by “select”? LAMMPS doesn’t have selections per se, just groups and atom-style variables.

Hi Park,

Did you have a look at the solutions proposed here?

Otherwise, that would be easy to do using an external tool, like bash or python.


As noted, this can be done (inefficiently) with a loop. Since this topic comes up occasionally and AFAICR there has not been a working example provided, here is one for a region:

region  select sphere 5 5 5 2
group   select empty
variable inside equal count(select,select)
variable n loop 10000

label jump
variable select index $(random(1,atoms,42524):%.0f)
group select id ${select}
if "${inside} == 0" then &
    "group select delete" &
    "variable select delete" &
    "next n" &
    "jump SELF jump"

print "selected ID is: ${select}"

This uses the count() variable function (with two arguments!) to see of an atom is both in the group “select” and the region “select”. The group is initially set to empty, then one atom with a random atom ID added. So the count will be either 0 or 1. If it is 0, the group and variable are deleted and a new random atom selected. If the count is 1, then the atom is in the region and the select variable will contain its atom ID.

For selecting atoms randomly from a group, the process is similar, but now a new group, that is the intersection of the “select” group and the desired group, needs to be constructed and then the count() function (with one argument) is used on that group.

The same can probably be constructed in a more efficient way using a python style variable and the LAMMPS python module, but that requires a bit more effort to do and to debug, so I leave it for somebody else to try out and report here.

Hi Michael,
I am trying to simulate displacement cascade by providing specific kinetic energy to a specific atom which is not near the boundary.

I appreciate for your suggestion. This helps.

I appreciate for your help. When I tried to run it, I got the following message:
ERROR: Label wasn’t found in input script

Then you obviously need to debug your input script. Please note that I was giving you an example to show you the basic principle of how to approach such a situation, not a cut-n-paste solution that will work without learning about the individual commands, and without the need of adapting it for your specific needs.

Thanks for the idea. It really helped.