Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RandomFrom assistance

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
Hello all,

In my program I need to collect all people who have a specific statuscode. Once these people are collected I need to randomly select each one and assign them to a specific group of no more than 30 people per group. So depending on the number of eligible people I could have up to 6 groups. I have located the RandomFrom function that returns a randomly selected element from an array (put the person's id number in the array), but I then need to remove it once it's been selected so it doesn't get selected twice and assign the Groupnumber and personnumber within the group. I'm also not sure how to initialize the random number generator. Any suggestions on how to accomplish this?

Thanks!

Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
hi Leslie,

I didn't know about the "RandomFrom" function, just always used "Random" function. To initialize random number generator there you would have to call "Randomize" procedure before.

Let me propose following code assumed the persons are stored in a StringList named PersonList and the Group members in an array[0..5] of StringLists called GroupMemberLists:


const
MaxMembers = 30;
var
g, p, i: integer;
begin
Randomize;
for g := 0 to high(GroupMemberLists) do
for p := 0 to MaxMembers-1 do
begin
if (PersonList.Count = 0) then exit; // dirty..
i := Random(PersonList.Count);
GroupMemberLists[g].Append(PersonList);
PersonList.Delete(i);
end;
end;


After that you should find inside each of the 6 GroupMemberLists maximum 30 people, and every people is assigned to exactly one group.

Of course you have to create the StringLists before.

good luck !
Roderich
 
Are StringLists just variables that I declare in the var section?

Thanks for the help!

Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
So I am having problems with the GroupMembersLists Array, specifically initializing the array based on the number of eligible people. Any idea what I'm doing wrong? Thanks for any help!

procedure TfrmMainMenu.CreatePanels2Click(Sender: TObject);
const
MaxMembers = 30;
var
GroupMembersLists : array of TStrings;
PersonList : TStrings;
g, p, i: integer;
termdate, jNum : string;
begin
PersonList:= TStringList.Create;
frmCreatePanel.ShowModal; //Get which panel to create
termdate := frmCreatePanel.cmbCPVenireList.Text;
JMSData.qryCreatePanel.ParamByName('TDATE').AsString := termdate;
JMSData.qryCreatePanel.Prepare;
JMSData.qryCreatePanel.Active := True; //gather all eligible jurors
While not JMSData.qryCreatePanel.Eof do
begin
with PersonList do begin //Add each juror number to PersonList
jNum := JMSData.qryCreatePanel.FieldByName('JURNUM').AsString;
Add(jNum);
end;
JMSData.qryCreatePanel.Next;
end;
// depending on number of eligible need different group sizes
if JMSData.qryCreatePanel.RecordCount <= 150 then SetLength(GroupMembersLists, 5)
else if JMSData.qryCreatePanel.RecordCount > 150 AND JMSData.qryCreatePanel.RecordCount <= 180 then
SetLength(GroupMembersLists, 6)
else if JMSData.qryCreatePanel.RecordCount > 180 Then SetLength(GroupMembersLists, 7);

Randomize;
for g := 0 to high(GroupMemberLists) do
for p := 0 to MaxMembers-1 do
begin
if (PersonList.Count = 0) then exit; // dirty..
i := Random(PersonList.Count);
GroupMemberLists[g].Append(PersonList);
PersonList.Delete(i);
end;
end; Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
I still need help on the above section, but I decided to try and get it to work with a static array and now I'm having problems with red section. I get an error: &quot;Incompatible types String and TStrings

procedure TfrmMainMenu.CreatePanels2Click(Sender: TObject);
const
MaxMembers = 30;
var
GroupMemberLists : array [0..6] of TStrings;
PersonList : TStrings;
g, p, i: integer;
termdate, jNum : string;
begin
PersonList := TStringList.Create;
frmCreatePanel.ShowModal; //Get which panel to create
termdate := frmCreatePanel.cmbCPVenireList.Text;
JMSData.qryCreatePanel.ParamByName('TDATE').AsString := termdate;
JMSData.qryCreatePanel.Prepare;
JMSData.qryCreatePanel.Active := True; //gather all eligible jurors
While not JMSData.qryCreatePanel.Eof do
begin
with PersonList do begin //Add each juror number to PersonList
jNum := JMSData.qryCreatePanel.FieldByName('JURNUM').AsString;
Add(jNum);
end;
JMSData.qryCreatePanel.Next;
end;
// depending on number of eligible need different group sizes
//if JMSData.qryCreatePanel.RecordCount <= 150 then SetLength(GroupMembersLists, 5)
//else if JMSData.qryCreatePanel.RecordCount > 150 AND JMSData.qryCreatePanel.RecordCount <= 180 then
//SetLength(GroupMembersLists, 6)
//else if JMSData.qryCreatePanel.RecordCount > 180 Then SetLength(GroupMembersLists, 7);

Randomize;
for g := 0 to high(GroupMemberLists) do
for p := 0 to MaxMembers-1 do
begin
if (PersonList.Count = 0) then exit; // dirty..
i := Random(PersonList.Count);
GroupMemberLists[g].Append(PersonList);
PersonList.Delete(i);
end;
end;

Please help!!

Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
Ok, I got the RED problem solved (it should be Append(PersonList.Text)! Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
hi Leslie,

I dont't think it should be
Append(PersonList.Text);

but instead
Append(PersonList);

sorry for that !

another tip: use TStringList variables instead of TStrings

Regards
Roderich
 
I hate this !!
This editor has stolen my brackets once again !

It should read
Append(PersonList);
 
Actually I have gotten it to work just like I needed it to:

procedure TfrmMainMenu.CreatePanels2Click(Sender: TObject);
const
MaxMembers = 30;
var
GroupMemberLists : array of TStrings;
PersonList : TStrings;
g, p, i, inttemp: integer;
jNum, PID, p1 : string;
begin
PersonList := TStringList.Create;
frmCreatePanel.ShowModal; //Get which panel to create
JMSData.qryCreatePanel.Prepare;
JMSData.qryCreatePanel.ParamByName('TERM').AsInteger := StrToInt(termdate);
JMSData.qryCreatePanel.Active := True; //gather all eligible jurors
inttemp := 0;
While not JMSData.qryCreatePanel.Eof do
begin
with PersonList do begin //Add each juror number to PersonList
jNum := JMSData.qryCreatePanel.FieldByName('JURNUM').AsString;
Add(jNum);
inttemp := inttemp + 1;
end;
JMSData.qryCreatePanel.Next;
end;
// depending on number of eligible need different group sizes

if inttemp <= 150 then SetLength(GroupMemberLists, 5)
else if inttemp > 180 Then SetLength(GroupMemberLists, 7)
else SetLength(GroupMemberLists, 6);

for g := 0 to high(GroupMemberLists) do
GroupMemberLists[g] := TStringList.Create;
Randomize;
for p := 1 to MaxMembers do
for g := 0 to high(GroupMemberLists) do
begin
if (PersonList.Count = 0) then begin
MessageDlg('There are no jurors to be processed',mtInformation,[mbOk], 0);
exit;
end;
i := Random(PersonList.Count);
jNum := PersonList.Strings;
JMSData.qryCreatePanel.SQL.Clear;
JMSData.qryCreatePanel.SQL.Add('UPDATE JMLIB/JMPMAIN SET PANELID = :pID');
JMSData.qryCreatePanel.SQL.Add('WHERE JURNUM = ' + jNum);
JMSData.qryCreatePanel.Prepare;
If p < 10 then p1 := '0' + IntToStr(p) else p1 := IntToStr(p);
PID := copy(termdate,3,6) + 'P0' + IntToStr(g+1) + 'J' + p1;
JMSData.qryCreatePanel.ParamByName('PID').AsString := PID;
JMSData.qryCreatePanel.ExecSQL;
PersonList.Delete(i);
end;
JMSData.qryCreatePanel.SQL.Add('SELECT JURNUM FROM JMLIB/JMPMAIN WHERE STATUSCD = ''WS'' AND PANELID = '''' AND TERMDATE = :TERM');
JMSData.qryCreatePanel.Active := False;
end;

It may not be the best way and I still have a few details to &quot;clean up&quot;, but it works! Thanks for your help!
Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top