FUNCTION random_string (
min_length_in IN PLS_INTEGER DEFAULT 1
, max_length_in IN PLS_INTEGER DEFAULT 100
, string_type_in IN VARCHAR2 DEFAULT NULL
)
RETURN maxvarchar2_aat
IS
BEGIN
RETURN DBMS_RANDOM.STRING (
string_type_in
, DBMS_RANDOM.VALUE (min_length_in, max_length_in)
);
END random_string;
But that returns just a single string. In this package, you will find programs that return a set of random values of the specified types as an associative array.
Note 1: You may want to convert this package to return nested tables so that you can call the random value generators inside a SELECT statement.
Note 2: Check out the way that I use a string-indexed collection to easily make sure that random values are unique.
How do I use it?
To make the functions as useful as possible, I provide arguments that allow you to specify the number of random values you need, whether or not you require distinct values (no repeats), and the range over which the values can vary.
When generating strings, you can also specify the type of string you desire. Oracle offers these options:
u - uppercase
l - lowercase
a - mixed case
x - mix of uppercase and digits
p - any printable character
So in the following block of code, I obtain a list of 500 random strings with a minimum of 6 characters and a maximum of 20 characters, using a mix of uppercase and digits:
DECLARE
l_strings randomizer.maxvarchar2_aat;
BEGIN
l_strings :=
randomizer.random_strings (count_in => 500
, min_length_in => 3
, max_length_in => 20
, string_type_in => 'x'
, distinct_values_in => TRUE
);
FOR indx IN 1 .. l_strings.COUNT
LOOP
DBMS_OUTPUT.put_line (l_strings (indx));
END LOOP;
END;
Try it out – I think you will agree these are pretty random-looking strings!