Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Getting a Random Choice


  • Please log in to reply
23 replies to this topic
Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

thanks very much! i'll test and report back...



Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

tried the script as follows but it runs without doing anything? unsure.png

#SingleInstance FORCE

; Create the empty array
Files := Object

; Write to the array:
Loop, Read, C:\MediaInfo\Play_List.txt

; This loop retrieves each line from the file, one at a time
    Files.Insert(A_LoopReadLine) ; Append this line to the array

Loop, % Files.MaxIndex() ; Files.MaxIndex() contains the maximum number of items in the Array
{
	File := Unique_RChoice(Files)
msgbox % File
	;RunWait,C:\Program Files\Windows Media Player\wmplayer.exe %File%
	;Run,"C:\Program Files\Windows Media Player\wmplayer.exe" %File% ; tried this, too!
	Run,%File%
	;Runwait is really unlikely to work here. You have to do something to figure out when to resume the 

loop when the track has finished
}

Unique_RChoice(Array){
	If (Array.MaxIndex() = 0 || Array.MaxIndex() = "")
		return, "ERROR"
	else {
		Random,Rand,1,% Array.MaxIndex()
		Ret := Array[Rand]
		Array.Remove(Rand)
		return, Ret
	}
}

 

@mrmike. Here it is. I'm not sure if it will work 100% properly. But i gave it a few test runs.


Choices := ["glow stick", "needle", "stop sign", "blouse", "hanger", "rubber duck", "shovel", "bookmark", "model car", "tampon", "rubber band", "tire swing", "sharpie", "picture frame", "photo album", "nail filer", "tooth paste", "bath fizzers", "tissue box", "deodorant ", "cookie jar", "rusty nail", "drill press", "chalk", "word search", "thermometer ", "face wash", "paint brush", "candy wrapper", "shoe lace", "leg warmers", "wireless control", "boom box", "quilt", "stockings", "card", "tooth pick", "shawl", "speakers ", "key chain", "cork", "helmet", "mouse pad", "zipper", "glasses", "lamp shade", "sketch pad", "gage", "plastic fork", "flag", "clay pot", "check book", "CD"]

Loop,% Choices.MaxIndex()
	MsgBox % Unique_RChoice(Choices)

Unique_RChoice(Array){
	If (Array.MaxIndex() = 0 || Array.MaxIndex() = "")
		return, "ERROR"
	else {
		Random,Rand,1,% Array.MaxIndex()
		Ret := Array[Rand]
		Array.Remove(Rand)
		return, Ret
	}
}

First you create an array. And then pass the array off as a parameter to the function.
It will return a random choice and remove it from the array. It will return "ERROR" either when the values run out or if the parameter passed to the function is not an array.



Menixator
  • Members
  • 744 posts
  • Last active: Sep 01 2015 02:54 PM
  • Joined: 10 Jul 2013

I made a typo in my orginal code. It should be

Files := Object()

Not

Files := Object


Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

this one works as expected. simple and fast. i am running this script using run, % a_loopfield, 1. have a piece of code in my archives that figures out when to resume the loop when the track has finished that i am going to add later...

 

Maybe, this is not enough code;)

fileread, x, mp3.txt
sort, x, Random
loop, parse, x, `n, `r
run, % a_loopfield, 1
    ;soundplay,% a_loopfield, 1
 

 



Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

thanks, it works now! i will add the part that figures out when to resume the loop when the track has finished now...

 

I made a typo in my orginal code. It should be

Files := Object()

Not

Files := Object


Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

this is the piece of code that figures out when to resume the loop when the track has finished:

p.s.: this is NOT my code. i believe it was created by Wicked.

{
o:=comobjcreate("Shell.Application")
;fileselectfile,file,c:\
;file := Clipboard
;msgbox %file%
splitpath,file,file,directory
if errorlevel
	exitapp
od:=o.namespace(directory)
of:=od.parsename(file)
loop,50 ; 50 is an arbitrary number > 21
	if(od.getdetailsof("",a_index)=="Duration"){
		length:=od.getdetailsof(of,a_index)
		break
	}
stringsplit,length,length,:
MP3Len := 3600000*length1+60000*length2+1000*length3
;msgbox,% "Duration of " file " in millisceonds is " 3600000*length1+60000*length2+1000*length3 "."
Return
}


Homeward Bound
  • Members
  • 1 posts
  • Last active: Jan 25 2016 11:25 AM
  • Joined: 30 Oct 2015

Hello, I'm new round here and think AHK is wonderful ...

 

I'm not sure if anyone else has noticed but in Menixator's excellent post there is a miss-spelling in the first line:

 

Random,Coice_Num,1,3
If (Choice_Num = 1)
    Choice = "Rock"
else if (Choice_Num = 2)
    Choice = "Num"
else if (Choice_Num = 3)
    Choice = "Num"

 

I assume Coice should be Choice.



hawai50
  • Members
  • 1 posts
  • Last active: Nov 09 2017 01:27 PM
  • Joined: 05 Nov 2015

Hi, I'm very new to AHK, so apologies if this exists but I can't find it. 

 

So I want to be able to randomise a script. At the moment I have:

 

::hi1::
Send, Hi
Return
 
::hi2::
Send, Hello
Return
 
::hi3::
Send, Hey
Return
 
Is there anyway to have something like this where a random response from that group is sent? I also have many other groups to create in the AHK script:
 
::hi::
Random Send, (Hi, Hello, Hey)
Return

 

::soz::

Random Send, (Oh no, I'm sorry, I can't believe it!)
Return

 

I hope that makes sense, 

 

Thanks, 

 

Luke



space
  • Members
  • 520 posts
  • Last active:
  • Joined: 12 Aug 2014
one way, posted elsewhere on the forum too
hi:=["Hi", "Hello", "Hey"]
soz:=["Oh no, I'm sorry", "I can't believe it!"]

::hi::
::soz::
abr:=SubStr(A_ThisHotkey,3)
Random, Word, 1, % %abr%.MaxIndex()
Send % %abr%[Word]
Return