• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Random numbers with double chance for some!

I need to write a procedure that generates 6 unique random numbers from 49. But I want to be able to double the chances of 3 selected number being generated. The first bit is easy using Random but how do i double the chance for the selected numbers?
1 Solution
OK, say your three numbers that you want to double the chance are:

5, 17, 22

Well, instead of looking between 1 and 49, look between 1 and 52.

If you get 50, then change it to 5
If you get 51, then change it to 17
If you get 52, then change it to 22

Assume you want to double the chance of a, b, and c:

Value := Random(49);
if not (Value in [a, b, c]) then
  OtherChance := Random(49);
  if OtherChance in [a, b, c] then
    Value := OtherChance;

Match_Set := [1, 2, 3];
iChance := 2;

iCnt := 0;
  iValue := Random(49);
  IF iValue IN Match_Set
    iCnt := iChance + 1
    iCnt := iCnt + 1;
UNTIL (iCnt >= iChance);

This is slightly similar to what Kambiz has done but is more generic allowing you to increase the number of chances that the 'special' numbers have of appearing. It also allows you to create the set a bit more intelligently (just construct the set before you enter the loop). In the above code you'd double the chance of selecting 1, 2, and 3, but by changing the iChance value to 3, you triple it etc.

The Neil
bennionAuthor Commented:
Thanks everyone all would have worked for me but TheNeil's look to be very flexible. It also solved my next problem which is to inccreae the chance of selection.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now