Problem Handling Random function

Manish_AhlawatManish_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;
Rule from today: Never take others comment in a wrong way, these might help you to travel a long way in your carrier.

Answers

  • garakgarak Member Posts: 3,263
    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!
  • suvidhasuvidha Member Posts: 117
    Use

    Randomize;
    amount := RANDOM(temp);
  • Manish_AhlawatManish_Ahlawat Member Posts: 31
    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.
  • matttraxmatttrax Member Posts: 2,309
    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.
  • Manish_AhlawatManish_Ahlawat Member Posts: 31
    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.
  • matttraxmatttrax Member Posts: 2,309
    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?
  • suvidhasuvidha Member Posts: 117
    Use
    Randomize(i);
  • Manish_AhlawatManish_Ahlawat Member Posts: 31
    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.
Sign In or Register to comment.