Subject Re: random
From Mervyn Bick <invalid@invalid.invalid>
Date Tue, 28 Jun 2016 17:03:48 +0200
Newsgroups dbase.getting-started
Attachment(s) golf_matches.prg

On 26/06/2016 14:53, Charlie wrote:
> Hi.. Could someone push me in the right direction?
>
> I have a database of say 40 golfers.  19 of those golfers are going to play in the upcoming tournament.  The 19 are selected so you have a filter with the 19 active golfers.  There are 5 golfers who are considered slow.  So I want to fit them in each group and possibly 2 at the end so play moves relatively smoothly.
>
> Then I would like to randomly place the rest of the golfers in a configuration of 3-somes and 4-somes with the 3-somes in the front of the group.  I'm not sure how to work this out in dbase.  I am writing this in oodml.  I have the database set up already.
>
> We have three tournaments a week and I want to make a schedule for each day, email each member that the tourney is scheduled and post the schedule to a web site in html.   I think I can figure this part out, but the order and grouping is not something I'm confident with. (or even have a clue!)
>
> Thanks for any help!
>

The attached example uses an algorithm normally used for shuffling decks
of cards in computer programs.

"Normal" players and "slow" players are extracted from the membership
table and added to two separate arrays.  These arrays, one with 14
entries and the other with 5 entries, are "shuffled" and then recombined
into one array.

I've set things up as 1 3-some  and 4 4-somes with matches 2,3,and 4
having one "slow" player and match 5  having 2 "slow" players.  If you
want a different arrangement of matches you will need to change the way
the interleaving is done.  You will also need to change the way the
match number is calculated.

An array is created for each of the three days and these are used to
create a table.  The table is overwritten every time the program is run
so be sure to make backup copy immediately.  If you destroy the table
there is no way of recreating it exactly.

Mervyn.











//create sample file for example
clear
close all
if file('shuffle.dbf')
//  drop table shuffle
endif

if not file('shuffle.dbf')
   create table shuffle  (id autoinc,name character(25), selected boolean,slow boolean)


   use shuffle
   for n = 65 to 84
      append blank
      replace name with chr(n)
   next
   for n = 97 to 116
      append blank
      replace name with chr(n)
   next
   go top
   for n = 1 to 19
      replace selected with true
      skip 2
      
   next
   go top
   for n = 1 to 5
      replace slow with true
      replace name with trim(name) + '  ***'
      skip 4
   next
   use
endif
*************************************************

******* create tables for each day's matches
  i = random(-1)
  aDay1 = build_array()
  aDay2 = build_array()  
  aDay3 = build_array()  
  

if file('matches.dbf')
   drop table matches
endif

if not file('matches.dbf')
   create table matches  (id autoinc,name character(25),day_no int,;
     match int)
  
   q = new query()
   q.sql = 'select * from matches'
   q.active = true  
   for nDay = 1 to 3
      nMatch = 1
      for n = 0 to 18
          q.rowset.beginAppend()
          cmd = "q.rowset.fields['name'].value = aDay"+nDay+"[n+1]"
          ?cmd
          &cmd
          q.rowset.fields['day_no'].value = nDay
          if n%4 = 3
             nMatch ++
           endif
           q.rowset.fields['match'].value = nMatch
       next
       q.rowset.save()      
    next
    q.active = false
endif    
  
  


   function build_array
      ***** Create an aray aPlayers1 for 14 normal players
      aPlayers1 = new array()
      q = new query()
      q.sql = 'select name from shuffle where selected = true and not slow = true'
      q.active = true
      do while not q.rowset.endofset
        aPlayers1.add(q.rowset.fields['name'].value)
        q.rowset.next()
      enddo
      q.active = false
//      if type('i') = 'U'
//      ?'i'
//         i = random(-1) //initlise random() by passing and argument. Only do this once.
//      endif
      for n = aPlayers1.size -1 to 1 step -1
         do  
           i = int(random()*15)
         until i>0 and i<15
         ?i
         a1 = aPlayers1[n]
         a2 = aPlayers1[i]
         aPlayers1[n] = a2
         aPlayers1[i] = a1
      next
      ****** Create and array aPlayers2 for slow players
      aPlayers2 = new array()
      q = new query()
      q.sql = 'select name from shuffle where selected = true and slow = true'
      q.active = true
      do while not q.rowset.endofset
        aPlayers2.add(q.rowset.fields['name'].value)
        q.rowset.next()
      enddo
      q.active = false
      i = random(-1)
      for n = aPlayers2.size -1 to 1 step -1
         do  
           i = int(random()*6)
         until i>0 and i<6
         a1 = aPlayers2[n]
         a2 = aPlayers2[i]
         aPlayers2[n] = a2
         aPlayers2[i] = a1
      next
      ******* Create an array aPlayers and interleave normal and slow players.
      **** Match 1      3 Ball   3 normal players
      **** Match 2,3,4  4 Ball   3 normal players  1 slow player
      **** Match 5      4 Ball   2 normal players  2 slow players


      //   1 X 3 ball, 4 X 4 ball
      i = 1
      aPlayers = new array()
      for n = 1 to 6
        aPlayers.add(aPlayers1[n])
      next
      aPlayers.add(aPlayers2[i])
      i ++  
      for n = 7 to 9
        aPlayers.add(aPlayers1[n])
      next  
      aPlayers.add(aPlayers2[i])
      i ++  
      for n = 10 to 12
        aPlayers.add(aPlayers1[n])
      next  
      aPlayers.add(aPlayers2[i])
      i ++  
      for n = 13 to 14
        aPlayers.add(aPlayers1[n])
      next  
      aPlayers.add(aPlayers2[i])
      i ++
      aPlayers.add(aPlayers2[i])
      return aPlayers