Re: PRAGMA Question

From: Thomas J. Kyte <tkyte_at_us.oracle.com>
Date: 1996/11/26
Message-ID: <329b6853.27205589_at_dcsun4>#1/1


They missed a pragma at the top of the package for specifying the package level purity when they copied my code. Here is my original package (what book is this in anyway? I've posted this random package a couple of times on this group but didn't realize anyone published it...). See the first pragma in this package. You need this cause the package has elaboration code (startup code) to initialize the seed.

create or replace package random
is

        pragma restrict_references( random, WNDS, RNPS );

        procedure srand( new_seed in number );

	function rand return number;
	pragma restrict_references( rand, WNDS  );

	procedure get_rand( r OUT number );

	function rand_max( n IN number ) return number;
	pragma restrict_references( rand_max, WNDS);

	procedure get_rand_max( r OUT number, n IN number );

end random;
/

create or replace package body random
is

	multiplier	constant number         := 22695477;
	increment	constant number        	:= 1;
	"2^32"		constant number			:= 2 ** 32;
	"2^16"		constant number         := 2 ** 16;
	"0x7fff"	constant number         := 32767;
	Seed		number := 1;
--
	procedure srand( new_seed in number ) 
	is
	begin
		Seed := new_seed;
	end srand;
--
	function rand return number
	is
	begin
		seed := mod( multiplier * seed + increment, "2^32" );
		return bitand( seed/"2^16", "0x7fff" );
	end rand;
--
	procedure get_rand( r OUT number ) 
	is
	begin
		r := rand;
	end get_rand;
--
	function rand_max( n IN number ) return number 
	is
	begin
		return mod( rand, n ) + 1;
	end rand_max;
--
	procedure get_rand_max( r OUT number, n IN number ) 
	is
	begin
		r := rand_max( n );
	end get_rand_max;
--
begin
	select userenv( 'SESSIONID' ) into seed from dual;
end random;
/





On Tue, 26 Nov 1996 15:54:55 GMT, xsah1_at_sunyit.edu (Scott Humphrey) wrote:


>I need some help regarding pragmas. I keyed a program directly from the
>Oracle PL/SQL Programming book for generating random numbers as follows:
>
>
>CREATE OR REPLACE PACKAGE RANDOM AS
>
> PROCEDURE ChangeSeed(p_NewSeed IN NUMBER);
>
> FUNCTION Rand RETURN NUMBER;
> PRAGMA RESTRICT_REFERENCES(rand,WNDS);
>
> PROCEDURE GetRand(p_RandomNumber OUT NUMBER);
>
> FUNCTION RandMax(P_MaxVal IN NUMBER) RETURN NUMBER;
> PRAGMA RESTRICT_REFERENCES(RandMax,WNDS);
>
> PROCEDURE GetRandMax(p_RandomNumber OUT NUMBER,
> p_MaxVal IN NUMBER);
>END Random;
>/
>CREATE OR REPLACE PACKAGE BODY RANDOM AS
>
> v_Multiplier CONSTANT NUMBER := 22695477;
> v_Increment CONSTANT NUMBER := 1;
> v_seed number := 1;
>
> Procedure ChangeSeed(p_NewSeed IN NUMBER) IS
> BEGIN
> v_seed := p_newSeed;
> END ChangeSeed;
>
> FUNCTION Rand RETURN NUMBER IS
> BEGIN
> v_seed := MOD(v_multiplier * v_seed + v_increment,(2 ** 32));
> RETURN BITAND (v_seed/(2 ** 16), 32767);
> END Rand;
>
> PROCEDURE GetRand (p_RandomNumber OUT NUMBER) IS
> BEGIN
> p_randomNumber := Rand;
> END GetRand;
>
> FUNCTION RandMax (p_MaxVal IN NUMBER) RETURN NUMBER IS
> BEGIN
> RETURN MOD (Rand,p_MaxVal) + 1;
> END RandMax;
>
> PROCEDURE GetRandMax(p_RandomNumber OUT NUMBER,
> p_MaxVal IN NUMBER) IS
> BEGIN
> p_RandomNumber := RandMax(P_MaxVal);
> END GetRandMax;
>
>BEGIN
> ChangeSeed(TO_NUMBER(TO_CHAR(SYSDATE,'SSSSS')));
>END Random;
>/
>
>This gives me the error
>
>PLS-00452 'rand' violates its associated pragma.
>
>Can someone explain why I get these pragma errors despite copying it
>right from the Oracle Press book.
>
>Thanks,
>
>Scott Humphrey
>Senior Programmer Analyst
>xsah1_at_sunyit.edu
>
>
Thomas Kyte Oracle Government tkyte_at_us.oracle.com http://govt.us.oracle.com ---- Check out Oracle Governments web site! ----- Follow the link to "Tech Center" and then downloadable Utilities for some free software... ------------------- statements and opinions are mine and do not necessarily reflect the opinions of Oracle Corporation
Received on Tue Nov 26 1996 - 00:00:00 CET

Original text of this message