Jump to content
Sign in to follow this  
Abyss

function/snipset ProbaRandom (Lấy số ngẫu nhiên có xác suất)

Recommended Posts

ProbaRandom
Author: Lev_Popov

I. GIỚI THIỆU CHUNG

Hàm này cho phép bạn lấy 1 số ngẫu nhiên với xác xuất

 II. Function

stock ProbaRandom(array[], size_w = sizeof(array))
{
	if(size_w < 1) return -1;
	new sum = 0, result = 0;
	for(new i = size_w - 1; i > -1; i--)
	{
		sum += array[i];
		if(random(sum) < array[i])
		{
			result = i; 
		}
	}
	return result; 
}

III. SỬ DỤNG

CMD:test(playerid, params[])
{
	new array[] = {5, 3, 15, 7, 50, 10}; 
	for(new i; i < 10; i++)
	{
		printf("%d", ProbaRandom(array));
	}
	return 1;
}

 

  • Like 3

Share this post


Link to post
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
Reply to this topic...

×   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.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...