#include <postgres.h>
#include <catalog/pg_type.h>
#include <utils/array.h>

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(random_array);

Datum
random_array(PG_FUNCTION_ARGS)
{
  int32 n = PG_GETARG_INT32(0);
  int32 lobound = PG_GETARG_INT32(1);
  int32 hibound = PG_GETARG_INT32(2);
  
  Datum *elems 	= palloc(sizeof(Datum) * n);
  ArrayType *retval;
  
  int i;
  
  for (i=0; i<n; i++)
    elems[i]	= (int32)random()%(hibound-lobound+1)+lobound;

  retval = construct_array(elems, n, INT4OID, 4, true, 'i');
  PG_RETURN_ARRAYTYPE_P(retval);
}
