## How to generate 5 random numbers without repeating?

- Juan Carlos
**Posts:**577**Joined:**Mon Sep 06, 2010 1:02 pm-
**Contact:**

### How to generate 5 random numbers without repeating?

How to generate 5 random numbers without repeating in Hollywood, it is easy in C but with Hollywood I have this little and simple doubt.

- airsoftsoftwair
**Posts:**3355**Joined:**Fri Feb 12, 2010 2:33 pm**Location:**Germany-
**Contact:**

### Re: How to generate 5 random numbers without repeating?

Code: Select all

```
For Local k = 0 To 4 Do DebugPrint(Rnd(range))
```

- Juan Carlos
**Posts:**577**Joined:**Mon Sep 06, 2010 1:02 pm-
**Contact:**

### Re: How to generate 5 random numbers without repeating?

I tested it and this easy routine generate repeated numbers, I tryed to convert this C++ routine that generates 5 number no repeat inside of array, but in the line where is the message Error this line, the routine stopped, I need a routine to get 5 number no repeat.Andreas wrote:Code: Select all

`For Local k = 0 To 4 Do DebugPrint(Rnd(range))`

For i=0 To i<6 Step 1

Num=Rnd(6)

If i>0

For j=0 To j<i Step 1

If Num<>vector[j] Error this line

Num=Rnd(6)

j=j-1

EndIf

Next

vector

*=Num*

EndIf

Next

EndIf

Next

### Re: How to generate 5 random numbers without repeating?

One way you could do it is by using a table scramble function. For example:
Now you can walk through the scrabled table from start to finish, and it will give you your random numbers without repeating. Obviously, that same p_ScrambleTable() can work with any kinds of lists (not just numbers). It might be nice to have an inbuilt function like that in some future version of Hollywood (Andreas?).

Code: Select all

```
Function p_ScrambleTable(InputTable$)
Local TempTable$=CopyTable(InputTable$)
Local NumItems=ListItems(TempTable$)
Local ReturnTable$={}
Local NewNum=NumItems
For Local x=1 To NumItems
InsertItem(ReturnTable$, RemoveItem(TempTable$, Rnd(NewNum)))
NewNum=NewNum-1
Next
Return(ReturnTable$)
EndFunction
RandomVar=5
TestTable$={}
For Local x=1 To RandomVar
InsertItem(TestTable$, x)
Next
NewTable$=p_ScrambleTable(TestTable$)
DebugPrint(Unpack(TestTable$))
DebugPrint(Unpack(NewTable$))
```

- airsoftsoftwair
**Posts:**3355**Joined:**Fri Feb 12, 2010 2:33 pm**Location:**Germany-
**Contact:**

### Re: How to generate 5 random numbers without repeating?

My first answer was nonsense of course, I should've read the question first Maybe I'll add such functionality but it shouldn't be so hard to implement it on your own.

### Re: How to generate 5 random numbers without repeating?

I'm obviously missing something, doesn't this
produce "5 random numbers without repeating" as asked in the first post?

Code: Select all

```
For Local k = 0 To 4
DebugPrint(Rnd(20))
Next
```

### Re: How to generate 5 random numbers without repeating?

I guess the point was that he wants 5 random numbers, without same result coming twice.

As example, if result is 3, then he wants the next rnd() result to be something else than 3.

As example, if result is 3, then he wants the next rnd() result to be something else than 3.

### Re: How to generate 5 random numbers without repeating?

What I posted does exactly that:

5,1,9,14,17;

4,11,5,17,8;

7,1,10,2,3… etc

5,1,9,14,17;

4,11,5,17,8;

7,1,10,2,3… etc

- airsoftsoftwair
**Posts:**3355**Joined:**Fri Feb 12, 2010 2:33 pm**Location:**Germany-
**Contact:**

### Re: How to generate 5 random numbers without repeating?

@youyise: But it's not guaranteed to behave like this. It could also return 5,1,5,14,17 or even 5,5,5,5,5 because the numbers are, well, random

### Re: How to generate 5 random numbers without repeating?

Oh right. I somehow managed to completely miss that part…