# Perl Predefined Numeric Functions

## Commonly Used Perl Predefined Functions – Part 2

### Perl Course

**Foreword: In this part of the series, I talk about Perl Predefined Numeric Functions that are commonly used.**

By: Chrysanthus Date Published: 19 Oct 2015

### Introduction

**The rand() Function**

The syntaxes of the rand function are:

rand EXPR

rand

EXPR is the argument, a number. If EXPR is omitted, the value of 1 is used. This function returns a random number greater than or equal to 0, and less than the value of EXPR. The random number may have a fraction (decimal part). Try the following code:

use strict;

my $var = rand(10);

print $var;

I tried it and I had,

2.2564697265625

If you tried the code, you would have a different number.

Now, note that the random number returned is not 100% random. It may be predicted. So it is not 100% secured. If you want a random number, which is hopefully secured, go to any of the following pages: http://search.cpan.org/perldoc/Data::Entropy, http://search.cpan.org/perldoc/Crypt::Random, http://search.cpan.org/perldoc/Math::TrulyRandom

**The abs() Function**

The syntaxes of this function are:

abs VALUE

abs

The argument is VALUE. If VALUE is absent, the value of $_ is used. This function will return a whole number from a signed number. Try the following code:

use strict;

my $val = abs(-5);

print $val;

The output is 5.

**The int() Function**

An integer is a whole number, which may be positive or negative. This function truncates a float (real) number and returns the integer part. That is, it throws away the decimal (fractional) part of a number and returns the integer part. It does not round the number. Try the following code:

use strict;

my $val = int(16.87);

print $val;

The output is 16.

**The sqrt() function**

This function returns the square root of a number. The syntaxes are:

sqrt EXPR

sqrt

EXPR is a number. If EXPR is omitted, the value of $_ is used (corresponding to the second syntax). Try the following code:

use strict;

my $val = sqrt(100);

print $val;

The output is, 10.

**The hex() Function**

This is the same function we saw in the previous part of the series. The syntaxes are:

hex EXPR

hex

EXPR is typically a string (single or double quotes) of hexadecimal digits. Hexadecimal digits are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f . This function converts a string of hexadecimal digits to the corresponding decimal (base 10) number. A string of hexadecimal digits is a hexadecimal number delimited by quotes. If EXPR is omitted, the value of $_ is used. Try the following code:

use strict;

my $val = hex('ff');

print $val;

The output is 255.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

### Related Links

Perl BasicsPerl Data Types

Perl Syntax

Perl References Optimized

Handling Files and Directories in Perl

Perl Function

Perl Package

Perl Object Oriented Programming

Perl Regular Expressions

Perl Operators

Perl Core Number Basics and Testing

Commonly Used Perl Predefined Functions

Line Oriented Operator and Here-doc

Handling Strings in Perl

Using Perl Arrays

Using Perl Hashes

Perl Multi-Dimensional Array

Date and Time in Perl

Perl Scoping

Namespace in Perl

Perl Eval Function

Writing a Perl Command Line Tool

Perl Insecurities and Prevention

Sending Email with Perl

**Advanced Course**

Miscellaneous Features in Perl

Perl Two-Dimensional Structures

Advanced Perl Regular Expressions

Designing and Using a Perl Module

**More Related Links**

Perl Mailsend

PurePerl MySQL API

Perl Course - Professional and Advanced

Major in Website Design

Web Development Course

Producing a Pure Perl Library

MySQL Course

**BACK**

**NEXT**