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

Random number generator for even numbers only (500 points)

I need to quickly generate a random number between 1000 and 65000 but it must be even.  

I am using Delphi 6.  

I am giving the maximum 500 points as this is urgent.  Many thanks for your help.
0
rincewind666
Asked:
rincewind666
1 Solution
 
SteveBayCommented:
I think this is what you want:
function TForm1.RandEven: Integer;
begin
     Randomize;
     repeat
          Result :=  (Random( 64000 ) + 1000);
     UNtil (Result Mod 2) = 0
 
end;

Open in new window

0
 
rincewind666Author Commented:
SteveBay, many thanks for your help.  Greatly appreciated.
0
 
TheVanterCommented:
Randomize;
numberwanted := Random(500 + random(32000)) * 2;

The randomize is needed to mix up the pseudo-random numbers generated by the time.

or for a variable even range:
function randomeven(min, max: double): double;
var range, tmp : double;
  begin
  Randomize;
  if min > max then
    begin
    tmp := min;
    min := max;
    max := tmp;
    end;
  min := min / 2;
  max := max / 2;
  range := max - min;
  randomeven := (min + Random * range) * 2;
  end;

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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