التمرين ببساطة انشاء دالة او وحدة لتوليد القيم العشواية بدون استخدام الدوال الجهزه فى دلفى
يكون بها بعض الخصائص
1- امكانية عدم التكرار(مثلا لاتعطينى رقم مرتان متتاليتان)
2- امكانية تحديد النسبة (مثلا **** True بنسبة 70% مثل ال RandomGenerator المدمج فى مكتبة ACM فى الجافا)
3- امكانية تحديد رقم بداية او seed
الهدف من التمرين
1-معرفة الفرق بين برمجة واحد واستخدمها -Clients and Implementors
2-معرفة كيفية توليد رقم شبة عشوائ وعدم وجود عشوائية او ابداع فى الحاسب الالى
مده التمرين اسبوع
///////////////////////////////////////////
/////// Random Generator by bo0oda ///////
/////// 31-8-2010 ///////
////////////////////////////////////////
unit RandomGenerator;
interface
uses
Windows;//only for GetTickCount;
type
TRandomGenerator = class(TObject)
private
LastBoolean : Byte;//0:True 1 : False 2:Nothing
lastValue : Extended;
function RandomNumber(Range : Extended):Extended;
public
RangeFrom : Extended;
RangeTo : Extended;
seed: Extended;
TruePercnt: Single;
Duplicate: Boolean;// defult is fulse = duplicate valuse ; true = non duplicate
function Next(): Integer;//Retrieves the next random integer
function NextDouble():Extended;//Retrieves the next random Extended
function NextBoolean(): Boolean;//Retrieves the next random Boolean
constructor Create();
end;
implementation
constructor TRandomGenerator.Create();
begin
RangeFrom:= 0;
RangeTo:= 100;
lastValue:= 0;
TruePercnt:= 50;;
lastValue:= 0;
LastBoolean:= 2;
end;
// new mod coz the result of orginal one is integer;
function _mod(Dividend,Divisor : Extended):Extended;
var
quotient: Extended;
begin
Quotient:= TRunc(Dividend) Div TRunc(Divisor);
Result:= Dividend - (Quotient * Divisor)
end;
// this function to give you random number in your range
function TRandomGenerator.RandomNumber(Range : Extended):Extended;
var
k : Int64;
begin
if Range <> 0 then //Checking if range != 0
begin
// set seed first
if seed = 0 then
seed:= GetTickCount;
//Math Operation
try
Result:= seed * 65536*1989.0115+0.1028 ;
Result:= _mod(Result,Range);
while Duplicate and (Result = lastValue) do
begin
Result:= seed * 65536*1989.0115+0.1028 ;
Result:= _mod(Result,Range);
seed:= (seed * seed) + 1;
end;
except
Result:= Range-1;
end;
lastValue:= Result;
seed:= lastValue;
end else Result:= 0; //if range = 0;
end;
function TRandomGenerator.NextDouble(): Extended;
begin
If RangeFrom > RangeTo then
Result:= RandomNumber(RangeFrom - RangeTo) + RangeTo
else
REsult:= RandomNumber(RangeTo- RangeFrom) + RangeFrom ;
end;
function TRandomGenerator.Next(): Integer;
begin
If RangeFrom > RangeTo then
Result:= Trunc(RandomNumber(RangeFrom - RangeTo) + RangeTo)
else
REsult:= Trunc(RandomNumber(RangeTo- RangeFrom) + RangeFrom) ;
end;
function TRandomGenerator.NextBoolean(): Boolean;
var
P: Single;
begin
if Duplicate and (LastBoolean <>2)then
begin
case LastBoolean of
0:begin
Result:= False;
LastBoolean:= 1;
end;
1:begin
Result:= True;
LastBoolean:= 0;
end;
end;
end else
begin
P:= RandomNumber(100);
Result := p < TruePercnt;
if Result then LastBoolean:= 0 else
LastBoolean:= 1;
end;
end;
توليد أرقام عشوائبة يتم تحديدها وتعبئة(ComboBox)
---------------------------------------------------------------
procedure Shuffle(var aArray; aItemCount: Integer; aItemSize: Integer);
{ after Julian M Bucknall }
var
Inx: Integer;
RandInx: Integer;
SwapItem: PByteArray;
A: TByteArray absolute aArray;
begin
if (aItemCount > 1) then
begin
GetMem(SwapItem, aItemSize);
try
for Inx := 0 to (aItemCount - 2) do
begin
RandInx := Random(aItemCount - Inx);
Move(A[Inx * aItemSize], SwapItem^, aItemSize);
Move(A[RandInx * aItemSize], A[Inx * aItemSize], aItemSize);
Move(SwapItem^, A[RandInx * aItemSize], aItemSize);
end;
finally
FreeMem(SwapItem, aItemSize);
end;
end;
end;
-------------------------------------------------------------
procedure TForm12.Button4Click(Sender: TObject);
var
a: array[1..6] of Integer;
i: Shortint;
III:INTEGER;
begin
ComboBox2.Clear;
for i := 1 to High(a) do a[i] := i;
Shuffle(a, High(a), SizeOf(Integer));
for i := 1 to 6 do
BEGIN
ComboBox2.Items.Add(IntToStr(a[i]));
END;
ComboBox2.ItemIndex:=0;