#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    6
    Rep Power
    0

    Smile Random Number - Easy?


    Hi,

    I hope this is an easy one. I am trying to generate a unique random number between 1 and 10. Since randomrange is not inclusive of the max value, I put 11 instead of 10.

    Here is the code:

    Code:
    number1 := randomrange(1,11);
    number2 := randomrange(1,11);
    number3 := randomrange(1,11);
    number4 := randomrange(1,11);
    number5 := randomrange(1,11);
    number6 := randomrange(1,11);
    number7 := randomrange(1,11);
    
    repeat number1:= randomrange(1,11) until number1 <> number2 or number3 or number4 or number5 or number6 or number7;
    repeat number2:= randomrange(1,11) until number2 <> number1 or number3 or number4 or number5 or number6 or number7;
    repeat number3:= randomrange(1,11) until number3 <> number1 or number2 or number4 or number5 or number6 or number7;
    repeat number4:= randomrange(1,11) until number4 <> number1 or number2 or number3 or number5 or number6 or number7;
    repeat number5:= randomrange(1,11) until number5 <> number1 or number2 or number3 or number4 or number6 or number7;
    repeat number6:= randomrange(1,11) until number6 <> number1 or number2 or number3 or number4 or number5 or number7;
    repeat number7:= randomrange(1,11) until number7 <> number1 or number2 or number3 or number4 or number5 or number6;
    Problem is, the program still is not generating a unique number. It does generate a number between 1 and 10, but it isn't unique. Normally there is one integer that is repeated twice.

    Thanks in advance for your help!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    14
    Rep Power
    0
    [The random number is not realy random.. in the real... it has a sequence predestined )!!
    well
    i dont can understande youvery well!!
    but if you is tryng generate a random number between 1 and 10...
    use random(9) +1.....
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    If you are trying to generate 7 unique random numbers, then neither randomrange() or random() will help you. Instead you need to do something like this:

    1. Initialize an array of numbers from 1..10
    2. Use random to shuffle this array around a few times.
    3. Pick the first 7 elements of the random array.

    This will guarantee that the numbers are unique and randomly shuffled.

    Here's how to do it:
    Code:
    var
    	intarray: array[1..10] of integer;
    	i : integer;
    	index, temp: integer;
    	number1, number2, number3, number4,
    	number5, number6, number7		: integer;
    begin
    	// Init the random number generator
    	Randomize;
    	// First set the initial values of intarray
    	for i := 1 to 10 do
    		intarray[i] := i;
    
    	// Now shuffle the array
    	for i := 1 to 10 do
    	begin
    		// Pick a random array index
    		index := random(9) + 1;
    		// Now exchange the elements
    		temp := intarray[index];
    		intarray[index] := intarray[i];
    		intarray[i] := temp;
    	end;
    
    	// Now read the first few elements of this shuffled array:
    	number1 := intarray[1];
    	number2 := intarray[2];
    	number3 := intarray[3];
                 ...
    	number7 := intarray[7];
    end;
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    6
    Rep Power
    0
    Thanks so much, proteus_adi and Scorpions4ever.

    Yes, that code works fine, Scorpions4ever. Thanks again.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    6
    Rep Power
    0

    Random Numbers


    Just finished implementing it and it works beautifully.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    6
    Rep Power
    0

    Smile Random Numbers - A problem in Delphi


    Hi,

    I had experienced the same problem during one of my projects. The random function generates duplicate values. I solved that problem by placing an array which stores the recently generated values and checks if the newly generated value is found in that array, it re-calls the random function, again repeating the same checking process for that new number. I know this is a bit complex but it works.

    Hope it works for you.

    Regards,
    Harish Save
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    Originally Posted by harishsave
    Hi,

    I had experienced the same problem during one of my projects. The random function generates duplicate values. I solved that problem by placing an array which stores the recently generated values and checks if the newly generated value is found in that array, it re-calls the random function, again repeating the same checking process for that new number. I know this is a bit complex but it works.

    Hope it works for you.

    Regards,
    Harish Save
    This isn't actually a very efficient solution, because you may have to generate a random number several times to get one unused number. This will start happening a lot towards the end.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    93
    Rep Power
    10
    harishsave,

    If you don't mind large number, just get the date/time in 100ns as your key. That should be sufficiently unique. If that's not enough, add ur network MAC as well.

IMN logo majestic logo threadwatch logo seochat tools logo