Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Random number generator for even numbers only (500 points)

Posted on 2008-06-16
3
217 Views
Last Modified: 2010-04-05
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
Comment
Question by:rincewind666
3 Comments
 
LVL 14

Accepted Solution

by:
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;

Open in new window

0
 

Author Comment

by:rincewind666
ID: 21797022
SteveBay, many thanks for your help.  Greatly appreciated.
0
 
LVL 2

Expert Comment

by:TheVanter
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;

Open in new window

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Intraweb download file link ? 1 153
Reconfigure Delphi Install? 2 60
Base1 Encode/Decode 3 81
Embarcadero C++ builder XE10.1 Berlin find In files dialogue stunted display 4 51
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question