Problem Handling Random function

Manish_Ahlawat
Member Posts: 31
Ok this is the code I am using with...
following variables ...
//i,idx are Integer
//Amount is an array and I want to sort it after inserting value with random function....
Now the problem I am facing is that everytime I run this the random function always generate the same random numbers...
Why is it so and what can I do to generate new random numbers everytime I run this code.
//This is the Coding is some one want to refer..
FOR i := 1 TO 10 DO BEGIN
amount := RANDOM(temp);
MESSAGE('Value of Amount(%1) = %2',i,amount);
END;
MESSAGE('Length of array=(%1)',ARRAYLEN(amount));
FOR i:= 1 TO 10 DO BEGIN
FOR idx := ARRAYLEN(amount) DOWNTO 2 DO
IF amount[idx] < amount[idx-1] THEN BEGIN
TempAmount := amount[idx];
amount[idx] := amount[idx-1];
amount[idx-1] := TempAmount;
END;
END;
FOR i := 1 TO 10 DO BEGIN
MESSAGE('Value of Amount(%1) = %2',i,amount);
END;
following variables ...
//i,idx are Integer
//Amount is an array and I want to sort it after inserting value with random function....
Now the problem I am facing is that everytime I run this the random function always generate the same random numbers...
Why is it so and what can I do to generate new random numbers everytime I run this code.
//This is the Coding is some one want to refer..
FOR i := 1 TO 10 DO BEGIN
amount := RANDOM(temp);
MESSAGE('Value of Amount(%1) = %2',i,amount);
END;
MESSAGE('Length of array=(%1)',ARRAYLEN(amount));
FOR i:= 1 TO 10 DO BEGIN
FOR idx := ARRAYLEN(amount) DOWNTO 2 DO
IF amount[idx] < amount[idx-1] THEN BEGIN
TempAmount := amount[idx];
amount[idx] := amount[idx-1];
amount[idx-1] := TempAmount;
END;
END;
FOR i := 1 TO 10 DO BEGIN
MESSAGE('Value of Amount(%1) = %2',i,amount);
END;
Rule from today: Never take others comment in a wrong way, these might help you to travel a long way in your carrier.
0
Answers
-
take a look (online help) to random and randomize.
If you need a really random number, u can also use a guid and eleminate there the chars like A..Z and {}Do you make it right, it works too!0 -
Use
Randomize;
amount := RANDOM(temp);0 -
suvidha wrote:Use
Randomize;
amount := RANDOM(temp);
@ Suvidha..
Now the problem I am facing is that even though random is now generating different value each time I run the code but every element of the amount array have the same value..
Please have a look at it..
maybee copy the code into a codeunit and try running it.Rule from today: Never take others comment in a wrong way, these might help you to travel a long way in your carrier.0 -
I don't see that you initialized temp anywhere. Look at the online help again. When you specify a parameter to the RANDOM function, that is the largest number you can get. If you have initialized your variable, it is going to be zero every time.0
-
matttrax wrote:I don't see that you initialized temp anywhere. Look at the online help again. When you specify a parameter to the RANDOM function, that is the largest number you can get. If you have initialized your variable, it is going to be zero every time.
temp :=100;
I did initialized but forgot to mention it..Rule from today: Never take others comment in a wrong way, these might help you to travel a long way in your carrier.0 -
temp := 100;
RANDOMIZE;
FOR i := 1 TO 10 DO BEGIN
amount := RANDOM(temp);
//MESSAGE('Value of Amount(%1) = %2',i,amount);
END;
That code generates different numbers every time I run it on my machine. Is that the same code that you have?0 -
Use
Randomize(i);0 -
matttrax wrote:temp := 100;
RANDOMIZE;
FOR i := 1 TO 10 DO BEGIN
amount := RANDOM(temp);
//MESSAGE('Value of Amount(%1) = %2',i,amount);
END;
That code generates different numbers every time I run it on my machine. Is that the same code that you have?
Yes it is working perfectly now...
Thanks a lot...Rule from today: Never take others comment in a wrong way, these might help you to travel a long way in your carrier.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions