How can I make Delphi 2 pick me any number within a range I specify (for example, between 0 and 100)?I’m guessing this is a fairly easy thing to do, but I couldn’t find it in the manual…

To get Delphi to pick any number within a specific range, you use thebuilt-in random number generator. Here’s a quick function to get you on yourway:

function GetRandomInt(UpperRange : Integer) : Integer;begin  Randomize;  Result := Random(UpperRange);end;
To get any number between 0 and 100, you’d call GetRandomInt, like so:
RandInteger := GetRandomInt(100);
So what’s going on in the code? The first line, Randomize, initializesDelphi’s random number generator. Then the second call, using theRandom function, sets the function’s result to a number between 0and the UpperRange specified.

A quick note about the Random function: It’s one of the very fewfunctions in Delphi that can be overloaded; that is, you can have a variablenumber of parameters. With Random, it’s either no parameters or an upperrange. If you don’t specify any parameters with Random (i.e. just declareRandom;) the function will return a Real number between 0 and 1. If youspecify an upper-range parameter, Random will return an integer between 0and the upper range.

You might be thinking, “Wait a minute! I thought Delphi didn’t support variable parameters otherthan open arrays.”

It doesn’t. But there are certain functions built into theDelphi engine that can take variable numbers of parameters. Random is one ofthem, and off the top of my head, I can’t name others. In any case, playaround with this. For another reference, look up Random in the help file.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes