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

Random Number Generation 2

Status
Not open for further replies.

acewilli

IS-IT--Management
Apr 11, 2003
98
US
Hello, I need to add people's name multiple times to pick one of them at random using the Random Number Function in VBScript. I know how to use it using only each person's name one time but how can I add each person multiple times so that they have a better chance of being picked by the Random Number Function? Below is my code so far which works great but each person is only added one time right now.

Code:
<%
Option Explicit
Response.Buffer = True

'For browsers (set expiration date some time well in the past)
Response.Expires = -1000

'For HTTP/1.1 based proxy servers
Response.CacheControl = "no-cache"

'For HTTP/1.0 based proxy servers
Response.AddHeader "Pragma", "no-cache"

Dim Hat(10)

Hat(1)  = "Ace"
Hat(2)  = "Charles"
Hat(3)  = "Johnny"
Hat(4)  = "Julie"
Hat(5)  = "Debbe"
Hat(6)  = "Thuan"
Hat(7)  = "Gary"
Hat(8)  = "Lisa"
Hat(9)  = "Eva"
Hat(10) = "Ramona"

Function RandomNumber(intHighestNumber)
	Randomize
	RandomNumber = Int(intHighestNumber * Rnd) + 1
End Function

Dim strRandomHat
strRandomHat = Hat(RandomNumber(10))
%>
<html>
<head>
  <title>Random Telecommute Drawing</title>
</head>
<body>
  <div style="border:thin solid black;margin:1em;
    padding:1em;height:100px;width:100%">

    <center><h1>Winner of the Random Telecommute Day is... </h1>
    <p><%= strRandomHat %><p></center>

  </div>
  <br />

  <form name="frmNewHat">
  <center><input type="hidden" name="cmdNewHat" value="Random Selection"
    onClick="window.open(document.location.href,'_top');" /></center>
  </form>
</body>
<center>
<FORM>
<INPUT TYPE="BUTTON" VALUE="Back" ONCLICK="window.location.href='../cssopt/home.asp'"> 
</FORM>
</center>
</html>
<% Response.End %>
 
You give a relative probablility for each candidate and conceptually find the inverse function of the monotonic cumulative density function. Practically, it is something like this for simulation.
[tt]
Dim Hat(10)
'Hat(0) is abandoned
Hat(1) = "Ace"
Hat(2) = "Charles"
Hat(3) = "Johnny"
Hat(4) = "Julie"
Hat(5) = "Debbe"
Hat(6) = "Thuan"
Hat(7) = "Gary"
Hat(8) = "Lisa"
Hat(9) = "Eva"
Hat(10) = "Ramona"
[green]
'assign non-negative number to each index, but index 0 zero.
dim p(10)
p(0)=0 'index 0 is abandoned
'just arbitrary numbers here for illustration
p(1)=0.5
p(2)=5.5
p(3)=0.3
p(4)=0.8
p(5)=4.0
p(6)=2
p(7)=0
p(8)=1.0
p(9)=0.5
p(10)=2.0

dim Norm : Norm=0
dim i
for i=0 to ubound(p)
Norm=Norm+p(i)
next

randomize 'take this statement outside of RandomNumber as well
function RandomNumber2(intHighestNumber)
dim k, i, cp
k=Int(intHighestNumber*Rnd)+1
cp=0
i=-1
do
i=i+1
cp=cp+p(i)/Norm
loop while k>cp*intHighestNumber
RandomNumber2=i
end function
[/green]
Function RandomNumber(intHighestNumber)
[red]'[/red]Randomize
RandomNumber = Int(intHighestNumber * Rnd) + 1
End Function
[/tt]
Then you change the call RandomNumber everywhere to RandomNumber2.
 
Okay...one more question. This worked great and you get a star for it but can you tell me why I can make it work with the Option Explicit? It works fine with it commented out which I'm very happy about but I'm worried that it is caching the results. Can you tell me if it is okay without the Option Explicit? Again...thank you so much...it works great just like I have it below. By the way, what I am doing with the added code is pulling the total of monthly points for each person. Works great! Thanks again!

Code:
<!-- #includes file="includes/dbconn.inc" -->
<%
'Option Explicit
Response.Buffer = True

'For browsers (set expiration date some time well in the past)
Response.Expires = -1000

'For HTTP/1.1 based proxy servers
Response.CacheControl = "no-cache"

'For HTTP/1.0 based proxy servers
Response.AddHeader "Pragma", "no-cache"

	strSQL = "Select * FROM Home"
	Set rsRandom = Server.CreateObject("ADODB.Recordset")
	rsRandom.open strSQL, db

set team1 =db.execute ("SELECT ttmopoints as hat1 from Home WHERE ID='1' ")
set team2 =db.execute ("SELECT ttmopoints as hat2 from Home WHERE ID='2' ")
set team3 =db.execute ("SELECT ttmopoints as hat3 from Home WHERE ID='3' ")
set team4 =db.execute ("SELECT ttmopoints as hat4 from Home WHERE ID='4' ")
set team5 =db.execute ("SELECT ttmopoints as hat5 from Home WHERE ID='5' ")
set team6 =db.execute ("SELECT ttmopoints as hat6 from Home WHERE ID='6' ")
set team7 =db.execute ("SELECT ttmopoints as hat7 from Home WHERE ID='7' ")
set team8 =db.execute ("SELECT ttmopoints as hat8 from Home WHERE ID='8' ")
set team9 =db.execute ("SELECT ttmopoints as hat9 from Home WHERE ID='9' ")
set team10 =db.execute ("SELECT ttmopoints as hat10 from Home WHERE ID='10' ")

Dim Hat(10)

Hat(1)  = "Ace"
Hat(2)  = "Charles"
Hat(3)  = "Johnny"
Hat(4)  = "Julie"
Hat(5)  = "Debbe"
Hat(6)  = "Thuan"
Hat(7)  = "Gary"
Hat(8)  = "Lisa"
Hat(9)  = "Eva"
Hat(10) = "Ramona"

'assign non-negative number to each index, but index 0 zero.
dim p(10)
p(0)=0    'index 0 is abandoned
'just arbitrary numbers here for illustration
p(1)=team1("hat1")
p(2)=team2("hat2")
p(3)=team3("hat3")
p(4)=team4("hat4")
p(5)=team5("hat5")
p(6)=team6("hat6")
p(7)=team7("hat7")
p(8)=team8("hat8")
p(9)=team9("hat9")
p(10)=team10("hat10")

dim Norm : Norm=0
dim i
for i=0 to ubound(p)
    Norm=Norm+p(i)
next

randomize    'take this statement outside of RandomNumber as well
function RandomNumber2(intHighestNumber)
    dim k, i, cp
    k=Int(intHighestNumber*Rnd)+1
    cp=0
    i=-1
    do
        i=i+1
        cp=cp+p(i)/Norm
    loop while k>cp*intHighestNumber
    RandomNumber2=i
end function

Function RandomNumber(intHighestNumber)
	'Randomize
	RandomNumber2 = Int(intHighestNumber * Rnd) + 1
End Function

Dim strRandomHat
strRandomHat = Hat(RandomNumber2(10))
%>
<html>
<head>
  <title>Random Telecommute Drawing</title>
</head>
<body>
  <div style="border:thin solid black;margin:1em;
    padding:1em;height:100px;width:100%">

    <center><h1>Winner of the Random Telecommute Day is... </h1>
    <p><%= strRandomHat %><p></center>

  </div>
  <br />

  <form name="frmNewHat">
  <center><input type="hidden" name="cmdNewHat" value="Random Selection"
    onClick="window.open(document.location.href,'_top');" /></center>
  </form>
</body>
<center>
<FORM>
<INPUT TYPE="BUTTON" VALUE="Back" ONCLICK="window.location.href='../cssopt/home.asp'"> 
</FORM>
</center>
</html>
<% Response.End %>
 
>why I can[not] make it work with the Option Explicit?
It is because you have to declare all the variables I introduced if you use Option Explicit. I added the necessary device for the new functionality and I think you should make good at least the declaration part as you are absolutely free what to call the new variables and function/sub anything you like better...
 
But it isn't causing an issue, correct? Would it do me any good to declare all of the variables since this is ASP? Sorry but I've never used Option Explicit in ASP and wasn't sure of why I would need to especially in this instance.

Thanks again....you really saved me on this. I was having a brain block on how to get this accomplished.
 
Option Explicit catches errors if you misspell a variable name (pretty common problem), and only variables that are Dimmed are allowed to be used in the script.

Lee
 
Thank you...I believe your comment will help others as well that are new to vbscript. Star for you. Is it my statements like below that I would need to dim the variables? team1 and hat1 or only team1?

Code:
set team1 =db.execute ("SELECT ttmopoints as hat1 from Home WHERE ID='1' ")

Thanks again!
 
Option Explicit applies to VBScript variables only. You would have to Dim team1 AND db (most likely done in the include file) for your code to work after declaring Option Explicit:

Code:
Option Explicit
Dim team1

'create db here.....
Set db = '..................

set team1 =db.execute ("SELECT ttmopoints as hat1 from Home WHERE ID='1' ")

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top