random select with ratio

# Thread: random select with ratio

1. Junior Member
Join Date
Dec 1969
Posts
21

## random select with ratio

there is a variable : color <BR>with values either : red , green , blue , black or white<BR>I want each time the page loads , to select a random value from those given , such that the ratio in times of selection is as follows :<BR>red = 20%<BR>green = 5%<BR>blue = 40%<BR>black = 18%<BR>white = 27%<BR><BR>so how can I do that?<BR><BR>thanx<BR>

2. Senior Member
Join Date
Dec 1969
Posts
1,843

## RE: random select with ratio

I would do this like this, but there may be a better way: <BR><BR>Create an array or database table that has 100 rows with each row having an associated color. Red would have 20 rows, green 100, etc. Then use a function to generate a random number that is between 0 and 99. Whatever number is pulled up, use that row from the array. <BR>

3. Senior Member
Join Date
Dec 1969
Posts
1,843

## RE: random select with ratio

I of course mean that red would have 20 rows, green 5, etc

4. Senior Member
Join Date
Dec 1969
Location
Indianapolis, IN
Posts
1,755

## RE: random select with ratio

The random part looks like this:<BR>&#060;% <BR>DIM intLow &#039; declare lowest number variable<BR>DIM intHigh &#039; declare highest number variable<BR>DIM intRandom &#039; declare the random number variable<BR>RANDOMIZE TIMER<BR>intLow = 1 &#039; set lowest number variable<BR>intHigh = 100 &#039; set highest number variable<BR>intRandom = Int((intHigh - intLow + 1) * RND + intLow<BR><BR>Then all you would need to do is something like this:<BR>If intRandom &#060;= 20 Then<BR>&#039;RED!!<BR>ElseIf intRandom &#062; 20 And intRandom &#060;= 25 Then<BR>&#039;GREEN!!<BR>ElseIf intRandom &#062; 25 And intRandom &#060;= 65 Then<BR>&#039;BLUE!!<BR><BR>Etc.

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Many ways to do this...

...but maybe the simplest to understand:<BR><BR>&#060;%<BR>&#039; assign weights to the colors...<BR>&#039; these can be percentages, but don&#039;t have to be...<BR>&#039; any kind of weighting you want to use works<BR>CONST WT_red = 20<BR>CONST WT_green = 5<BR>CONST WT_blue = 40<BR>CONST WT_black = 18<BR>CONST WT_white = 27 <BR><BR>Randomize<BR><BR>num = Int( (WT_red+WT_green+WT_blue+WT_black+WT_white) * Rnd() )<BR>If num &#060; WT_red Then<BR> color = "red"<BR>Elseif num &#060; WT_red+WT_green Then<BR> color = "green"<BR>Elseif num &#060; WT_red+WT_green+WT_blue Then <BR> color = "blue" <BR>Elseif num &#060; WT_red+WT_green+WT_blue+WT_black Then<BR> color = "black"<BR>Else<BR> color = "white"<BR>End If<BR>%&#062;<BR><BR>If this is something that would be done dozens or hundreds of times per page, I might get fancier than that. But as it is? This works. And you can change weightings by simply changing the CONST values.<BR>

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## The overhead of creating the array...

...isn&#039;t worth it if you will do this only once per page.<BR><BR>Though I suppose you could put the array into an Application variable and then just use it on each page. Hmmm...sure! Why not.

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## Ummm...okay, now explain...

...why you need to test the intRandom twice per ElseIf??<BR><BR>If intRandom &#060;= 20 Then<BR> &#039;RED!!<BR>ElseIf intRandom &#062; 20 And intRandom &#060;= 25 Then<BR> &#039;GREEN!!<BR><BR>How do you *get* to the green-colored line if intRandom is *not* greater than 20??? Didn&#039;t the first IF say "I&#039;m processing all the intRandom values that are &#060;= 20"?<BR><BR>Not a big deal for something like this, of course. Just something to think about if you ever need a high performance test like this one.<BR><BR>

8. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## What i would do

if the randon number is from <BR>1 to 20 then Red<BR>21 to 25 then green<BR>blah blah<BR><BR><BR>so no array just a few regular IFs<BR><BR>

9. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## Oh....oh well

same thing<BR><BR><BR>

10. Senior Member
Join Date
Dec 1969
Location
Indianapolis, IN
Posts
1,755

## Not sure what you're pointing out...

If I have a random number between 1 and 100, and I want Red to represent any number between 1 and 20, I would use:<BR>If intRandom &#060;= 20<BR><BR>If Green is to represent any number between 21 and 25, I would use:<BR>ElseIf intRandom &#062; 20 And intRandom &#060;= 25<BR><BR>I&#039;m doing this similar to your method. If I had a value of CONST_Red = 20 and CONST_Green = 5, then my elseif would be:<BR>ElseIf intRandom &#062; CONST_Red And intRandom &#060;= CONST_Red + CONST_Green<BR><BR>I didn&#039;t think of using the constants, though. <BR>But I don&#039;t understand what you&#039;re trying to point out.<BR>You don&#039;t get the green line if intRandom is less than or equal to 20. If intRandom &#060;= 20, then we want Red as the value.

#### Posting Permissions

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