Page 1 of 2

How to generate 5 random numbers without repeating?

Posted: Wed Feb 05, 2014 11:52 am
by Juan Carlos
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.

Re: How to generate 5 random numbers without repeating?

Posted: Wed Feb 05, 2014 4:27 pm
by airsoftsoftwair

Code: Select all

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

Re: How to generate 5 random numbers without repeating?

Posted: Fri Feb 07, 2014 12:20 am
by Juan Carlos
Andreas wrote:

Code: Select all

For Local k = 0 To 4 Do DebugPrint(Rnd(range))
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.
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

Re: How to generate 5 random numbers without repeating?

Posted: Sat Feb 08, 2014 9:26 pm
by PEB
One way you could do it is by using a table scramble function. For example:

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$))
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?).

Re: How to generate 5 random numbers without repeating?

Posted: Fri Feb 14, 2014 11:20 pm
by airsoftsoftwair
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?

Posted: Mon Feb 17, 2014 2:55 am
by youyise
I'm obviously missing something, doesn't this

Code: Select all

For Local k = 0 To 4
	DebugPrint(Rnd(20))
Next
produce "5 random numbers without repeating" as asked in the first post?

Re: How to generate 5 random numbers without repeating?

Posted: Mon Feb 17, 2014 5:21 am
by Bugala
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.

Re: How to generate 5 random numbers without repeating?

Posted: Wed Feb 19, 2014 5:44 am
by youyise
What I posted does exactly that:
5,1,9,14,17;
4,11,5,17,8;
7,1,10,2,3… etc

Re: How to generate 5 random numbers without repeating?

Posted: Wed Feb 19, 2014 6:34 pm
by airsoftsoftwair
@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?

Posted: Wed Feb 19, 2014 11:48 pm
by youyise
Oh right. I somehow managed to completely miss that part… :oops: :oops: :oops: :oops: :oops: