Oracle, Web, Script, SQLserver, Tips & Trick

October 28, 2010

Package RANDOM pada Oracle PL/SQL

Filed under: oracle — sikathabis @ 3:56 am
Tags:

Source Code
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;
/

Cara Menggunakannya
declare
nomor number;
begin
random.get_rand(nomor);
      — maka, variabel nomor adalah angka acak
      — fungsi2 yg lain bisa anda coba sendiri
end;

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Teknologi Pendidikan

Just another WordPress.com weblog

KATAKAMI

Jurnalisme Yang Dapat Dipercaya

Tonk Kosonk Baonk Bunyinya

Penampungan Serapah Tanpa Sumpah

Qey050489's Blog

Ayo Bangkit sobat..!!!!!

B. S. Totoraharjo

Orang Bodoh Yang Tak Kunjung Pandai

culturekitchen foodlab

New Food and Art in Yogyakarta

Wahyu Catur Wibowo

Musafir Dunia Maya

SQL.... Still Learning

My Reference on SQL Server

neinframe

Street Photography is like a box of a chocolates, you're never know what you're gonna get - Thomas Leuthard -

Keluarga Andrivito

-- Andrivito's Journal --

DRK™

This site is about Latest NEWS, Articles, Videos, Books, Course, Presentations, Tips & Tricks related to I.T Pro's

Gaurav Saxena

|| तत् त्वम् असि ||

Be a good Moeslim

Menuju Pribadi Muslim Sejati yang Bertaqwa

TuanSUFI

Manajemen Sholat, renungan spiritual, Teknologi Informasi

Pelangi di khatulistiwa....

Mari berbagi kebaikan, pengalaman dan senyum denganku

angkasa13

Ceritanya saya, Pikirannya saya, Hatinya saya

sikathabis

sikathabis semua informasi disini

%d bloggers like this: