Weighted Random Numbers?

1. Peter Shifrin Guest

## Weighted Random Numbers?

Hi,<BR><BR>I need to randomly pick a number from 1 to 5 but have the results be weighted. Example:<BR><BR>1 comes up 20% of the time.<BR>2 comes up 30% of the time.<BR>3 comes up 20% of the time.<BR>4 comes up 15% of the time.<BR>5 comes up 15% of the time.<BR><BR>What ASP code would accomplish this?<BR><BR>Thanks!

2. jd
Senior Member
Join Date
Dec 1969
Posts
850

## thimk

Well, one easy way would be to pick a number (call intRand) from 1 to 100, then check range like this:<BR><BR>&nbsp;&nbsp;&nbsp; if intRand &#062; 0 and intrand &#060; 21 then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; intNewRand = 1<BR>&nbsp;&nbsp;&nbsp; elseif intRand &#062; 20 and intRand &#060; 51 then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; intNewRand = 2<BR>&nbsp;&nbsp;&nbsp; elseif intRand &#062; 50 and intRand &#060; 71 then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; intNewRand = 3<BR>&nbsp;&nbsp;&nbsp; elseif intRand &#062; 70 and intRand &#060; 86 then <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; intNewRand = 4<BR>&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; intNewRand = 5<BR>&nbsp;&nbsp;&nbsp; end if<BR><BR>

3. New ASP Guest

## Easy!!!

DIM int_final<BR>DIM int_temp_int<BR><BR>Randomize<BR>int_temp_int = Int(20*RND()+1)<BR><BR>SELECT CASE int_temp_int<BR>CASE 1,2,3,4 &#039--&#062;20%<BR>int_final = 1 <BR>CASE 5,6,7,8,9,10 &#039--&#062;30%<BR>int_final = 2 <BR>CASE 11,12,13,14 &#039--&#062;20%<BR>int_final = 3<BR>CASE 15,16,17 &#039--&#062;15%<BR>int_final = 4 <BR>CASE 18,19,20 &#039--&#062;15%<BR>int_final = 5<BR>END SELECT<BR><BR><BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•