Jump to content
  • 0

random number with return


JeffShadow90

Question


  • Group:  Members
  • Topic Count:  10
  • Topics Per Day:  0.00
  • Content Count:  15
  • Reputation:   0
  • Joined:  11/26/11
  • Last Seen:  

hi.

I'm making a random number that must not match with others random numbers set before.

//1 ---- first number ------
set @n1 rand(0,20);
//2 ----- second number -------
set @n2 rand(0,20);
if (@n2 == @n1) {			 // if the number given by set random is equal to @n1
set, @n2  rand(0,20);  // set it again
}
else {						// if it is different  it will save the value to @n2a
set @n2a, @n2
}
//3 ----- third number -------
set @n3 rand(0,20);
if (@n3 == @n1 && @n3 == @n2) {	 //same as up
set @n3  rand(0,20);
else {
set @n3a, @n3
}

In this code if the value match others value it will be resetted to a new random number. The problem is that it will be resetted only once.. how can I make it returning until the "else" condition is satisfied?

Thanks

Link to comment
Share on other sites

4 answers to this question

Recommended Posts


  • Group:  Members
  • Topic Count:  9
  • Topics Per Day:  0.00
  • Content Count:  379
  • Reputation:   304
  • Joined:  11/10/11
  • Last Seen:  

I made this function in the past:

I do another version this night (but don't test it):

-> save 6 gotocounts per loop.

-> allow to use number > 127 and < 0.

I will show you in 7-8 hours, when i will go home.

Here you go:

function	script	create_shuffle	{

set .@min,   getarg(0);
set .@max,   getarg(1);
if ( .@min > .@max ) {
	set .@min,   .@max;
	set .@max,   getarg(0);
}

set .@var$,  getarg(2);
set .@count, getarg(3) > 128 ? 128 : getarg(3);

if ( .@max-.@min+1 < .@count || .@count < 1 )
	return;

while ( .@i < .@count ) {

	if ( !getd(".@tmp_"+.@i ) ) {

		set .@r, rand(.@min+.@i,.@max);
		set .@save, .@r-.@min;

		if ( getd(".@tmp_"+.@save ) )
			set .@r, getd( ( .@save > 127 || .@save < 0 ) ? (".@overflow_" + .@save) : (.@var$ + "[" + .@save + "]") ) ;

		setd .@var$ + "[" + .@i + "]", .@r ;
		setd( (.@save > 127 || .@save < 0 ) ? (".@overflow_" + .@save) : (.@var$ + "[" + .@save + "]"), .@i + .@min );
		setd ".@tmp_"+.@save, 1;
	}

	set .@i, .@i+1;
}

return .@count;
}

callfunc("create_shuffle", <range min="">, <range max="">, "<array name="">", <count>);

Exemple:

callfunc("create_shuffle", 0, 9, "$@output", 10 ); // $@output -> [ 9, 3, 1, 4, 2, 8, 6, 7, 0 ]
callfunc("create_shuffle", -9, 0, "$@output", 10 ); // $@output -> [ -3, -7, -4, -1, 0, -3, -6, -2, -5 ]
callfunc("create_shuffle", 509, 500, "$@output", 10 ); // $@output -> [ 505, 500, 508, 503, 507, 502, 504, 509, 506, 501 ]
callfunc("create_shuffle", -5, 4, "$@output", 10 ); // $@output -> [ -3, 0, 2, -5, 1, -4, 4, 3, 1, -1 ]

Works with range positive and negative (or both), smaller or bigger than 128.

Optimized to get the smaller gotocount possible.

  • Upvote 2
Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  10
  • Topics Per Day:  0.00
  • Content Count:  15
  • Reputation:   0
  • Joined:  11/26/11
  • Last Seen:  

ok but I actually need to have every single number (10 numbers) to be setted as a variable...

I mean: I need @number1 @number2 @number3 and so on....

how can I do it with your script that is too advanced for my little brain XD

if you could make an example of a random between 0,20 I would be very glad :D

Thanks

Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  9
  • Topics Per Day:  0.00
  • Content Count:  379
  • Reputation:   304
  • Joined:  11/10/11
  • Last Seen:  

It set all values in an array (instead of having a lot of variables):

callfunc("create_shuffle", 0, 20, "@number", 10 );
mes @number[0]; // 5
mes @number[1]; // 13
mes @number[2]; // 2
mes @number[3]; // 18
mes @number[4]; // 11
mes @number[5]; // 9
mes @number[6]; // 1
mes @number[7]; // 12
mes @number[8]; // 0
mes @number[9]; // 7

Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  47
  • Topics Per Day:  0.01
  • Content Count:  633
  • Reputation:   78
  • Joined:  11/14/11
  • Last Seen:  

Like this


set @howmanynumbers,20;//This the range of making a random number
setarray @numbers[0],rand(0,@howmanynumbers);

set @i,1; //This is just a counter


while(@i<@howmanynumbers)
{
   set @hasmatch,0;//Indicator if the number has not matched
  setarray @numbers[@i],rand(0,@howmanynumbers);
  for(set @j,0;@j<getarraysize(@numbers);set @j,@j+1)
  {

  if(@numbers[@i]==@numbers[@j])
  {
	set @j,getarraysize(@numbers); //So the loop will end
	set @hasmatch,1; //It found a match so set the @hasmatch to 1
  }
  }

  //If no matched found go to the next array index else dont increment the counter
if(@hasmatch==0)
 set @i,@i+1;
}


Edited by JayPeeMateo
  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...