![]() |
|
[تمرين] مولد ارقام عشوائية (RandomGenerator) - نسخة قابلة للطباعة +- delphi4arab منتديات دلفي للعرب (https://delphi4arab.net/forum) +-- قسم : منتديات دلفي للعرب التعليمية (https://delphi4arab.net/forum/forumdisplay.php?fid=3) +--- قسم : تمارين دلفي وحلول (https://delphi4arab.net/forum/forumdisplay.php?fid=16) +--- الموضوع : [تمرين] مولد ارقام عشوائية (RandomGenerator) (/showthread.php?tid=2363) |
[تمرين] مولد ارقام عشوائية (RandomGenerator) - bo0oda - 30-08-2010 التمرين ببساطة انشاء دالة او وحدة لتوليد القيم العشواية بدون استخدام الدوال الجهزه فى دلفى يكون بها بعض الخصائص 1- امكانية عدم التكرار(مثلا لاتعطينى رقم مرتان متتاليتان) 2- امكانية تحديد النسبة (مثلا **** True بنسبة 70% مثل ال RandomGenerator المدمج فى مكتبة ACM فى الجافا) 3- امكانية تحديد رقم بداية او seed الهدف من التمرين 1-معرفة الفرق بين برمجة واحد واستخدمها -Clients and Implementors 2-معرفة كيفية توليد رقم شبة عشوائ وعدم وجود عشوائية او ابداع فى الحاسب الالى مده التمرين اسبوع [تمرين] مولد ارقام عشوائية (RandomGenerator) - khiro.alg - 30-08-2010 فكرة تمرين ممتازة - بارك الله فيك للعلم أنه لاتوجد أية طريقة ناجحة غير إستخدام الوقت فالكمبيوتر لا يمكنه إعطاء قيم عشوائية لأن ذلك يعتبر ذكاء [تمرين] مولد ارقام عشوائية (RandomGenerator) - kachwahed - 30-08-2010 @khiro.alg إقتباس :للعلم أنه لاتوجد أية طريقة ناجحة غير إستخدام الوقتليس تماما، انظر: كود : http://en.wikipedia.org/wiki/Random_number_generationأرفق نموذج حل للتمرين (دون سورس طبعا). [تمرين] مولد ارقام عشوائية (RandomGenerator) - bo0oda - 31-08-2010 فى المرفقات نموذج للحل [تمرين] مولد ارقام عشوائية (RandomGenerator) - bo0oda - 02-09-2010 هل من حلول؟ من الممكن انشاء المولد على شكل TObject [تمرين] مولد ارقام عشوائية (RandomGenerator) - khiro.alg - 10-09-2010 لكن بالإعتماد على أشياء أخرى فإن ذلك ليس جيدا : - إما أنه لن يتخطى قيم معينة - أو أه يبقى يكرر بعض القيم - الوقت هو الشيء الوحيد الذي لا يتكرر إطلاقا [تمرين] مولد ارقام عشوائية (RandomGenerator) - bo0oda - 08-10-2010 هذا حل وليس الامثل كود : ///////////////////////////////////////////
/////// 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;
end.RE: [تمرين] مولد ارقام عشوائية (RandomGenerator) - osama23485555 - 08-04-2018 توليد أرقام عشوائبة يتم تحديدها وتعبئة(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; end; RE: [تمرين] مولد ارقام عشوائية (RandomGenerator) - حسن لطفى - 30-03-2020 يمكنك تحديد مجال العشوائية يمكنك اختزان الارقام الناتجة فى مصفوفة x يمكنك مقارنه الارقام الجديدة مع المصفوفة x لتجنب التكرار |