Selecting a random name from a list

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
guyash2
Junior Coder
Posts: 40
Joined: Thu May 05, 2005 2:45 pm

Selecting a random name from a list

Post by guyash2 » Mon Jun 13, 2005 11:09 am

Hi,
I want to write a macro which I give him a list of names and it selects a name from the list randomaly, and then it writes the name with the command "send".
How do I write a macro which selects a random name from a defined list?

thanks.

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Mon Jun 13, 2005 11:22 am

Here's a simple example:

Let>Names[1]=Fred
Let>Names[2]=Sally
Let>Names[3]=Harry
Let>Names[4]=John
Let>Names_Count=4

Random>4,rndnum
Let>rndnum=rndnum+1

Let>outname=Names[%rndnum%]
Send>outname
MJT Net Support
[email protected]

guyash2
Junior Coder
Posts: 40
Joined: Thu May 05, 2005 2:45 pm

exactly what I looked for

Post by guyash2 » Mon Jun 13, 2005 11:48 am

It works!
thank you very much

User avatar
machinogodzilla
Newbie
Posts: 17
Joined: Sun Jul 30, 2006 11:32 pm

Post by machinogodzilla » Sun Jul 30, 2006 11:50 pm

Hi!

I would like to complicate this problem a little bit. What to do if you want to eliminate the name which has been choicen randomly from the list of the names that it will not apper in the next draw? And this way until all the names will have been used. E.g. we have got four names in above example thus after the first draw there will be only three names remaining. After the second one two and so on.

Thanks!

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Mon Jul 31, 2006 4:05 am

Someone will probably come along with a 3 line solution, but here's one quick way to do it. Note that this is very simple randomization suitable for deciding who takes out the garbage, but not for a serious application.

//fill the Names array

Let>Names[1]=Fred
Let>Names[2]=Sally
Let>Names[3]=Harry
Let>Names[4]=John

//create the num array size = max and fill with numbers from 1 to max
Let>max=4
Let>n=1
label>start
Let>num[%n%]=%n%
Let>n=n+1
If>n>max,done,start
label>done

//shuffle the numbers by stepping through the array and swapping the
//number in each position with the number from a randomly selected
//position. You can do this step as many times as you like.

Let>randmax=max-1
Let>q=1
Label>shuffle
random>randmax,p
Let>p=p+1
Let>temp=num[%q%]
Let>num[%q%]=num[%p%]
Let>num[%p%]=temp
Let>q=q+1
If>q>max,done2,shuffle
Label>done2


//use the shuffled numbers in the num array to pull names from the Names array

Let>k=0
Repeat>k
Let>k=k+1
Let>r=num[%k%]
MessageModal>Names[%r%]
Until>k,max

Label>finish

User avatar
machinogodzilla
Newbie
Posts: 17
Joined: Sun Jul 30, 2006 11:32 pm

Post by machinogodzilla » Mon Jul 31, 2006 9:10 pm

Thank you for your ultrafast reply! It helped much :)

One more question yet. Is it possible to put into an array a cursor's position? If so how to do it and how to return the values from the array?

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Mon Jul 31, 2006 10:28 pm

machinogodzilla wrote:Thank you for your ultrafast reply! It helped much :)

One more question yet. Is it possible to put into an array a cursor's position? If so how to do it and how to return the values from the array?
I'm not sure what you mean :?

User avatar
machinogodzilla
Newbie
Posts: 17
Joined: Sun Jul 30, 2006 11:32 pm

Post by machinogodzilla » Mon Jul 31, 2006 11:30 pm

:) Let me put it this way. Instead of Sally or Harry from the above example I would like to have cursor's positions on the screen (x,y) preset by me and put to an array (if possible). From these places I would like the macro to choice one of them randomly, make a click, then eliminate, choice another place, click, eliminate and so on until there are no places left in the array.

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Aug 01, 2006 12:01 am

I think the simplest way would be to use an array, e.g. cursorpos[], and store the coordinates with a delimiter eg for x=100, y = 300 Let>cursorpos[1]=100|300

Then you would use Separate> to split the coordinates back out into x and y

Separate>cursorpos[1],|,gopos

then gopos_1 would be the first coord and gopos_2 would be the second so you could use those coords with MouseMove>

MouseMove>%gopos_1%,%gopos_2%

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Aug 01, 2006 6:46 am

Or use two arrays:

Let>cursorpos_x[1]=100
Let>cursorpos_y[1]=300
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
machinogodzilla
Newbie
Posts: 17
Joined: Sun Jul 30, 2006 11:32 pm

Post by machinogodzilla » Tue Aug 01, 2006 1:47 pm

Thank you very much! That is exactly what I was hoping to exist in msched :D

I was trying to use two arrays before but because of the randomisation I could not make it. I was receiving each array shuffled different way. And I needed matching pairs. But with the 'separate' command and the rest that became a piece of cake :wink: Thx!

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Aug 01, 2006 2:30 pm

You can shuffle two arrays in sync, but it's slower (not an issue with these tiny arrays) and I still think the other way is simpler.

Let>randmax=max-1
Let>q=1
Label>shuffle
random>randmax,p
Let>p=p+1
Let>tempx=xpos[%q%]
Let>tempy=ypos[%q%]
Let>xpos[%q%]=xpos[%p%]
Let>ypos[%q%]=ypos[%p%]
Let>xpos[%p%]=tempx
Let>ypos[%p%]=tempy
Let>q=q+1
If>q>max,done2,shuffle
Label>done2

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts