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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Select / Lucky Dip Question

Status
Not open for further replies.

Sheltered

Programmer
Nov 26, 2002
55
GB
Hello, i have created a web application using ASP for a fantasy football (soccer) type game.
Its a pretty straight forward system where users log-in and select players from a list stored on a database table.
To develop it further i'd like to add the ability for the user to be able to click a button and have a team automatically and randomly generated for them.
I know how to do random selects for just one player but i cant figure out the best way of doing a team. There are obviously rules for the game which need validating, i.e. team value can be no more than £30million and you must have a 1-4-3-3 formation.
Any ideas on the best way to go about this? Anyone done anything like it before?
I know more details on set up etc are probably needed but i thought i'd ask before posting any details.

Thanks in advance.
Pete
 
You are going to need to build arrays and populate them using loops with IF THEN - NEXT algorithms in the loops.

i.e. choose a player, where does he/she play, is there a slot in that position, fill it - if not, get a different player, fill position, get total team value.

It would be good to keep track in an array of what players you have already used or tried so you don't have test them again - save CPU

If team value > $30m, work out by how much, drop a player and fill space with another.

Or build a random team no matter what value and get person to have to make a couple of changes to get under salary cap.

Steve Davis
hey.you@hahaha.com.au

Me? I can't even spell ASP!
 
Thanks Microbe, you've pointed me in the right direction.
I've made a start and I now can select 11 random players validated against position.
I haven't managed to stop the query selecting duplicate records though :( The selected players id's are stored in an array, how would i go about stoping the select command picking any of the players in the array?
Also i've done it by looping the select command however many times i need per position. I've been reading up and i think this can be done better to save server resources using .GetRows() , what do you think?
The code i have so far is below.
Thanks for your help.


<!-- #include file=&quot;db.asp&quot; -->
<%Response.Buffer = True%>

<html>
<head>
<style>
p { font-family:verdana; font-size:11px; }
</style>
</head>
<body>
<br>
<p align=&quot;center&quot;>
<%
' ADO Constant. Dont change this
Const adCmdText = &H0001

' Define variables
Dim query
Dim rs
Dim intRnd
Dim playerId(11)
Dim counter
counter = 0


' Select random Goalkeeper
playernumber = 1
Do until playernumber = 2

' SQL statement
query = &quot;SELECT Play_Id,Play_Name,Play_Position,Play_Price FROM Players_Data WHERE Play_Position='Goalkeeper'&quot;

' Opening database
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.Open query, conn, 3, , adCmdText

' Generating random number from total number of records
Randomize Timer
intRnd = (Int(RND * rs.RecordCount))

' Now moving the cursor to random record number
rs.Move intRnd

' Showing the random player
Response.Write &quot;<b>&quot; & rs(&quot;Play_Position&quot;) & &quot;:</b> &quot; & rs(&quot;Play_Name&quot;) & &quot; £&quot; & rs(&quot;Play_Price&quot;) & &quot;million<br />&quot;

' Add player id to array
playerId(counter) = rs(&quot;Play_Id&quot;)
counter = counter + 1

' Closing the database
rs.Close

Set rs = Nothing
playernumber = playernumber + 1
Loop



' Select random Defenders
playernumber = 1
Do until playernumber = 5

' SQL statement
query = &quot;SELECT Play_Id,Play_Name,Play_Position,Play_Price FROM Players_Data WHERE Play_Position='Defender'&quot;

' Opening database
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.Open query, conn, 3, , adCmdText

' Generating random number from total number of records
Randomize Timer
intRnd = (Int(RND * rs.RecordCount))

' Now moving the cursor to random record number
rs.Move intRnd

' Showing the random player
Response.Write &quot;<b>&quot; & rs(&quot;Play_Position&quot;) & &quot;:</b> &quot; & rs(&quot;Play_Name&quot;) & &quot; £&quot; & rs(&quot;Play_Price&quot;) & &quot;million<br />&quot;

' Add player id to array
playerId(counter) = rs(&quot;Play_Id&quot;)
counter = counter + 1

' Closing the database
rs.Close

Set rs = Nothing
playernumber = playernumber + 1
Loop



' Select random Midfielders
playernumber = 1
Do until playernumber = 4

' SQL statement
query = &quot;SELECT Play_Id,Play_Name,Play_Position,Play_Price FROM Players_Data WHERE Play_Position='Midfielder'&quot;

' Opening database
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.Open query, conn, 3, , adCmdText

' Generating random number from total number of records
Randomize Timer
intRnd = (Int(RND * rs.RecordCount))

' Now moving the cursor to random record number
rs.Move intRnd

' Showing the random player
Response.Write &quot;<b>&quot; & rs(&quot;Play_Position&quot;) & &quot;:</b> &quot; & rs(&quot;Play_Name&quot;) & &quot; £&quot; & rs(&quot;Play_Price&quot;) & &quot;million<br />&quot;

' Add player id to array
playerId(counter) = rs(&quot;Play_Id&quot;)
counter = counter + 1

' Closing the database
rs.Close

Set rs = Nothing
playernumber = playernumber + 1
Loop



' Select random Strikers
playernumber = 1
Do until playernumber = 4

' SQL statement
query = &quot;SELECT Play_Id,Play_Name,Play_Position,Play_Price FROM Players_Data WHERE Play_Position='Striker'&quot;

' Opening database
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.Open query, conn, 3, , adCmdText

' Generating random number from total number of records
Randomize Timer
intRnd = (Int(RND * rs.RecordCount))

' Now moving the cursor to random record number
rs.Move intRnd

' Showing the random player
Response.Write &quot;<b>&quot; & rs(&quot;Play_Position&quot;) & &quot;:</b> &quot; & rs(&quot;Play_Name&quot;) & &quot; £&quot; & rs(&quot;Play_Price&quot;) & &quot;million<br />&quot;

' Add player id to array
playerId(counter) = rs(&quot;Play_Id&quot;)
counter = counter + 1

' Closing the database
rs.Close

Set rs = Nothing
playernumber = playernumber + 1
Loop
%>
</p>
<br /><br />
<%
Dim iLoop
For iLoop = LBound(playerId) to UBound(playerId)
Response.Write playerId(iLoop) & &quot;<br />&quot;
Next
%>
</body>
</html>
 
GetRows() is absolutely the way to go, particularly because then you won't even need to copy them into another array -- just use the array you get from GetRows() to peform your random selections.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top