How to generate 5 random numbers without repeating?

Find quick help here to get you started with Hollywood
User avatar
Juan Carlos
Posts: 889
Joined: Mon Sep 06, 2010 1:02 pm

How to generate 5 random numbers without repeating?

Post 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.
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: How to generate 5 random numbers without repeating?

Post by airsoftsoftwair »

Code: Select all

For Local k = 0 To 4 Do DebugPrint(Rnd(range))
User avatar
Juan Carlos
Posts: 889
Joined: Mon Sep 06, 2010 1:02 pm

Re: How to generate 5 random numbers without repeating?

Post 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
PEB
Posts: 569
Joined: Sun Feb 21, 2010 1:28 am

Re: How to generate 5 random numbers without repeating?

Post 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?).
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: How to generate 5 random numbers without repeating?

Post 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.
youyise
Posts: 10
Joined: Wed Jan 29, 2014 4:15 am

Re: How to generate 5 random numbers without repeating?

Post 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?
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: How to generate 5 random numbers without repeating?

Post 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.
youyise
Posts: 10
Joined: Wed Jan 29, 2014 4:15 am

Re: How to generate 5 random numbers without repeating?

Post by youyise »

What I posted does exactly that:
5,1,9,14,17;
4,11,5,17,8;
7,1,10,2,3… etc
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: How to generate 5 random numbers without repeating?

Post 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 :)
youyise
Posts: 10
Joined: Wed Jan 29, 2014 4:15 am

Re: How to generate 5 random numbers without repeating?

Post by youyise »

Oh right. I somehow managed to completely miss that part… :oops: :oops: :oops: :oops: :oops:
Post Reply