### Thread: Random Number - Easy?

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

Join Date
Sep 2004
Posts
6
Rep 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. 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.....
3. 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;```
4. 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.
5. 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.
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2004
Posts
6
Rep 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. 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. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2004
Posts
93
Rep Power
13
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.