Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 8th, 2004, 05:32 AM
 Spongebob316
Registered User

Join Date: Sep 2004
Posts: 6
Time spent in forums: 37 m 9 sec
Reputation Power: 0
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.

#2
September 8th, 2004, 11:50 AM
Registered User

Join Date: Aug 2004
Posts: 14
Time spent in forums: < 1 sec
Reputation 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.....

#3
September 8th, 2004, 02:10 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,534
Time spent in forums: 2 Months 3 Days 4 h 29 m 12 sec
Reputation Power: 4106
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

#4
September 9th, 2004, 02:19 AM
 Spongebob316
Registered User

Join Date: Sep 2004
Posts: 6
Time spent in forums: 37 m 9 sec
Reputation Power: 0
Thanks so much, proteus_adi and Scorpions4ever.

Yes, that code works fine, Scorpions4ever. Thanks again.

#5
September 9th, 2004, 04:32 AM
 Spongebob316
Registered User

Join Date: Sep 2004
Posts: 6
Time spent in forums: 37 m 9 sec
Reputation Power: 0
Random Numbers

Just finished implementing it and it works beautifully.

#6
September 23rd, 2004, 09:23 AM
 harishsave
Registered User

Join Date: Sep 2004
Posts: 6
Time spent in forums: 1 h 6 m 19 sec
Reputation Power: 0
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

#7
September 23rd, 2004, 01:21 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,534
Time spent in forums: 2 Months 3 Days 4 h 29 m 12 sec
Reputation Power: 4106
Quote:
 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.

#8
December 5th, 2004, 10:04 AM
 tinyabs
Contributing User

Join Date: Dec 2004
Posts: 93
Time spent in forums: 13 h 24 m 34 sec
Reputation 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.

 Viewing: Dev Shed Forums > Programming Languages - More > Delphi Programming > Random Number - Easy?