Exclude Set from Random Possibilities

Thread: Exclude Set from Random Possibilities

1. Senior Member
Join Date
Dec 1969
Posts
204

Exclude Set from Random Possibilities

I need to select a random Number, but one which must exclude numbers given a set of a variable number of items. Say there are 6 items, of which 2 and 5 have been selected, then my random number cannot be a 2 or a 5. I need to display 1,3,4,or 6. (The bricks to exclude will never be more than three.)<BR>I have code here of the exluded items, and then below a set that may be included.<BR><BR>How would I be able to get only the random numbers that I need?<BR><BR>//Bricks to Exclude<BR>for (x=0; x &#060; (count0); x++)<BR>{<BR>char[] splitter = {&#039;/&#039;};<BR>string[] avoidMe = avoidPromoBricks.Split(splitter);<BR>avoidThisBric k = Convert.ToInt32(avoidMe[x]);<BR> <BR>if (x==0)<BR>{<BR>avoidThisBrick1 = Convert.ToInt32(avoidMe[x]);<BR>}<BR>if (x==1)<BR>{<BR>avoidThisBrick2=Convert.ToInt32(avo idMe[x]);<BR>}<BR>if (x==2)<BR>{<BR>avoidThisBrick3=Convert.ToInt32(avo idMe[x]);<BR>} <BR>}<BR>//End Bricks To Exclude<BR>//Begin Bricks to Include<BR>for (int i = 0; i &#060; countPromoBricks; i++)<BR>{<BR>if (i != avoidThisBrick1)<BR>if (i != avoidThisBrick2)<BR>if (i != avoidThisBrick3<BR>{ <BR>includeThisBrick = i;<BR>}<BR>}<BR>//End Bricks to Include<BR><BR><BR>//Now get random number that exludes exlude Bricks<BR> Int randomInt = (1,Total + 1);

2. Senior Member
Join Date
Dec 1969
Posts
204

Totally Elementary Solution , Next ....

int randomizer = rand.Next(1, countPromoBricks + 1);<BR> <BR>while (randomizer == avoidThisBrick1 &#124&#124 randomizer == avoidThisBrick2 &#124&#124 randomizer == avoidThisBrick3)<BR>{<BR>randomizer = rand.Next(1, countPromoBricks + 1);<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
•