Selecting a Random atom from a Particular group

I am trying a pick a random atom from a group using a loop. Here’s the script.

label pick_pka
# generate random atom ID
variable tmr timer
variable SEED equal ceil(${tmr}*10000000)
variable natoms equal count(all)
variable trial equal floor(random(1,${natoms}+1,${SEED}))
print "Selected atom ID = ${trial}"
# make temporary group for this atom ID
group trialAtom id ${trial}
# check if this atom is in shell
group test intersect trialAtom shellAtoms
variable ntest1 equal count(test)
variable ntest equal ${ntest1}
group trialAtom clear
group test clear
if "${ntest} == 0" then "jump SELF pick_pka"
# success → assign PKA ID
variable PKA_id equal ${trial}
group pka id ${PKA_id}

and Here’s the output

15195 atoms in group shellAtoms
Selected atom ID = 200141
1 atoms in group trialAtom
0 atoms in group test
Selected atom ID = 176726
1 atoms in group trialAtom
0 atoms in group test
Selected atom ID = 145097
1 atoms in group trialAtom
0 atoms in group test
Selected atom ID = 197536
1 atoms in group trialAtom
0 atoms in group test
Selected atom ID = 51308
1 atoms in group trialAtom
0 atoms in group test
Selected atom ID = 48153
1 atoms in group trialAtom
0 atoms in group test
Selected atom ID = 82049
1 atoms in group trialAtom
0 atoms in group test
Selected atom ID = 235794
1 atoms in group trialAtom
0 atoms in group test
Selected atom ID = 237260
1 atoms in group trialAtom
0 atoms in group test
Selected atom ID = 240563
1 atoms in group trialAtom
0 atoms in group test
Selected atom ID = 115407
1 atoms in group trialAtom
0 atoms in group test
Selected atom ID = 123959
1 atoms in group trialAtom
0 atoms in group test
Selected atom ID = 94326
1 atoms in group trialAtom
0 atoms in group test
Selected atom ID = 201682
1 atoms in group trialAtom
0 atoms in group test
Selected atom ID = 201125
1 atoms in group trialAtom
0 atoms in group test
Selected atom ID = 38673
1 atoms in group trialAtom
0 atoms in group test
Selected atom ID = 176746
1 atoms in group trialAtom
1 atoms in group test
1 atoms in group pka
Selected PKA atom ID = 33887

My problem is that why the variable’s value changed here at the end? Am I missing something here? I am running this script with this following command line.

mpiexec -np 1 lmp.exe -sf omp -pk omp 1 -in in.lmp

Thank you for your time.

Because every time you expand ${trial} you get a new random value.
So even that atom ID for the group and the atom ID you output are different.
Just look at the detailed log for expansion in the log.lammps file.

Here is an example showing how to do avoid this:

variable SEED index 42323
region box block -5 5 -5 5 -5 5
region sph sphere 0 0 0 3

create_box 1 box
lattice fcc 1.0
create_atoms 1 box

group sph region sph
variable subset equal count(sph)
variable natoms equal count(all)
variable trial equal floor(random(1,${natoms}+1,${SEED}))

group trial empty
group test empty
print "group sph has ${subset} atoms of ${natoms}"

label again

group trial delete
group test delete
variable selected equal ${trial}
group trial id ${selected}
group test intersect sph trial
variable test equal count(test)
print "test = ${test} with atom ID ${selected}"
if "${test} == 0" then "jump SELF again"