Home AMX User Forum NetLinx Studio

Random Number Generator

I need to somehow come up with a random number generator for a non-malicious purpose. What I'm trying to do is have my program cause an Alcorn McBride 8-Traxx player to play tracks at random based on other input events. Basically, a button push would trigger a play command, but I want to randomly pick what track is to be played. It's not possible to randomly play the tracks on the 8-Traxx box itself...all I can do is send serial strings to tell it what track numbers to play. My only other alternative would be to just call the files in a seemingly random order and hope nobody notices the pattern, which is highly likely as these sound files are quite short and there are not that many of them, and I want it to be truly random anyway.

Comments

  • ericmedleyericmedley Posts: 4,177
    There is a random number generator
    This from the help file.

    Num = RANDOM_NUMBER(1000) // 0 <= Num < 1000

    Then use any of the stock sorts to create a random/unique list of numbers and go from there.

    Hope that helps
  • Joe HebertJoe Hebert Posts: 2,159
    You may want to try out the RANDOM_NUMBER function that's built into Netlinx.
  • Joe HebertJoe Hebert Posts: 2,159
    Shuffle This

    If you want to play all the tracks in your list randomly and not have any tracks repeat until all tracks are played once then you might find the following code useful.

    Compile, download, and then watch nShuffledPlaylist in the debugger. Every time you push button 1 you?ll see the playlist get shuffled.

    I?m sure there are different and better ways to shuffle but this is the approach I?ve used in the past.
    DEFINE_DEVICE
    
    dvTP	= 10001:1:0
    
    DEFINE_CONSTANT
    
    INTEGER nMaxTracks = 10
    
    DEFINE_VARIABLE
    
    INTEGER nOriginalPlaylist[] = {1,2,3,4,5,6,7,8,9,10}
    INTEGER nShuffledPlaylist[] = {1,2,3,4,5,6,7,8,9,10}
    
    INTEGER nNextTrack	//the next track to be played
    
    DEFINE_FUNCTION fnShuffleThis (integer nItems[]) {
          
       INTEGER x		 //looping variable
       INTEGER shufflecount //how many times to shuffle entire list
       INTEGER index	 //current position in the list
       INTEGER rnd		 //random position in the list
       INTEGER temp	 	 //temporary container so swap can be made
       INTEGER len		 //length of list passed in
    
       len = LENGTH_ARRAY(nItems) 		    //find out how long the list is
       shufflecount = RANDOM_NUMBER(11)+10	    //get a random number between 10-20
       
      
       FOR (x=1; x<=shufflecount; x++) {	    //shuffle the entire list shufflecount times
          FOR (index=1; index<=len; index++) {  //index through the list from start to end
    	 rnd = RANDOM_NUMBER(len)+1 	    //generate a random number between 1 and the number of items in list
    	 temp = nItems[index]		    //make a copy of the data that is stored at the indexed position
    					    //let's make the swap
    	 nItems[index] = nItems[rnd]	    //replace the indexed position item with the random positioned item
    	 nItems[rnd] = temp		    //take the indexed positioned item and store it in the random position
          }
       }
       
    }
    
    DEFINE_FUNCTION fnPlayNextTrack() {
    
       //Do whatever to play track nShuffledPlaylist[nNextTrack]
       nNextTrack++
       IF (nNextTrack>nMaxTracks) fnResetPlaylist()
    }
    
    DEFINE_FUNCTION fnResetPlaylist () {
    
       nNextTrack = 1
       
       //don't really need this next line, it's only for demo purposes
       //but if you wanted to change the set of tracks to play you could do it here
       nShuffledPlaylist = nOriginalPlaylist
    
       fnShuffleThis(nShuffledPlaylist)
       
       
    }
    
    DEFINE_EVENT
    
    BUTTON_EVENT[dvTP,1] {
    
       PUSH: {
          fnResetPlaylist()
       }
    }
    

    HTH
  • GeoGeo Posts: 6
    Thanks

    Thanks guys...these did the trick.
Sign In or Register to comment.