Solved

# Random number generator for even numbers only (500 points)

Posted on 2008-06-16
212 Views
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
Question by:rincewind666

LVL 14

Accepted Solution

SteveBay earned 500 total points
ID: 21796928
I think this is what you want:
function TForm1.RandEven: Integer;

begin

Randomize;

repeat

Result :=  (Random( 64000 ) + 1000);

UNtil (Result Mod 2) = 0

end;
0

Author Comment

ID: 21797022
SteveBay, many thanks for your help.  Greatly appreciated.
0

LVL 2

Expert Comment

ID: 21797079
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;
0

## Featured Post

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.