Math, pick five numbers to add to 10.

What i want to do is a bit complicated. Basically, i have a number, let’s say 10.
Now, i want to RANDOMLY generate 5 numbers, wich if you add them result in 10.
Examples:

1 + 2 + 3 + 3 + 1 = 10

1 + 1 + 1 + 1 + 6 = 10

4 + 1 + 2 + 1 + 2 = 10

… and so on.
Is there a way to do this?

==================================SOLVED ==============================================

I made my own Script, based on Fattie’s one. You can adjust the Number, in wich it should
result, how many numbers should be used and how many times he will go through the script:

#pragma strict
import System.Collections.Generic;

private var posts : List.<int>;
private var Result = new Array();

public var RunThroughs : int = 1000;
public var AmountOfNumbers : int = 5;
public var WantedNumber : int = 10;
   
function Start()
{	
    fiveNonZeroForTenShowHowPretty();
}

function fiveNonZeroForTenShowHowPretty()
{
	for (var k : int = 1; k <= RunThroughs; k++)
    {
       posts = new List.<int>();
 
       posts.Add(0);
       posts.Add(WantedNumber);
	   AddMoreNumbers();
	   
       posts.Sort();
       Display();
       
       if(Result.length == AmountOfNumbers)
       {
       		Debug.Log("RunThrough: " + k + 
       		" | " + "WantedNumber: " + WantedNumber + 
       		"    ---    " + "Result: " + Result);
       }
    }
}

function _unique0to10()
{
    var r : int = 0;
   
    if (posts.Count >= WantedNumber + 1)
    {
    	return 0;
    } 
     
    while(posts.Contains(r))
    {
    	r = Random.Range(0,WantedNumber + 1);
    }
    return r;
}
  
function AddMoreNumbers()
{
	for(var i : int = 0; i < AmountOfNumbers - 1; i++)
	{
		posts.Add( _unique0to10() );
	}
}  
 
function Display()
{
	for(var i : int = 0; i < AmountOfNumbers; i++)
	{
		 Result _= (posts[i + 1] - posts*);*_

* } *
}
Attach the Script to any GameObject and run the game. The Results will be in the Console.

#It is incredibly easy to do this…
.

This is a classic problem in algorithms, the “fencepost” problem.

Here’s the natty solution:

10719-posts.png

Just choose the four random fenceposts (the purple marks)


It’s funny, I use this as a test for young people screaming at me about how they want to Make Games and be a real programmer.

If you can “see” in your head instantly that what you’re actually trying to do is find four simple numbers (not five difficult numbers), you’ve “got” algorithms in your head! :slight_smile:

This is 'real" programming - all the stuff about syntax, etc., is fluff, right? :slight_smile:


#It is incredibly easy to do this…
.

here’s ALL OF THE CODE that matters … it is so so simple:

posts.Add(0);
posts.Add(10);
posts.Add( Random.Range(0,11) );
posts.Add( Random.Range(0,11) );
posts.Add( Random.Range(0,11) );
posts.Add( Random.Range(0,11) );
posts.Sort();

Here’s a fully working, teaching example.

drop it in a file called demo.js, and attach it to any game object, and look atyour console.

Note how beautiful and random the output groups are.

#pragma strict
import System.Collections.Generic;

function Awake()
	{
	fiveForTen();
	
	fiveNonZeroForTen();
	
	fiveNonZeroForTenShowHowPretty();
	}

var posts:List.<int>;
function fiveForTen()
	{
	posts = new List.<int>();
	
	posts.Add(0);
	posts.Add(10);
	
	posts.Add( Random.Range(0,11) ); // nb, that's 0->10 inclusive
	posts.Add( Random.Range(0,11) );
	posts.Add( Random.Range(0,11) );
	posts.Add( Random.Range(0,11) );
	
	posts.Sort();
	
	print("the fenceposts are at...");
	Debug.Log( posts[0] +" "+ posts[1] +" "+ posts[2] +" "+ posts[3] +" "+ posts[4] +" "+ posts[5] );
	
	print("the five results are obviously...");
	Debug.Log(
	(posts[1]-posts[0]) +" "+
	(posts[2]-posts[1]) +" "+
	(posts[3]-posts[2]) +" "+
	(posts[4]-posts[3]) +" "+
	(posts[5]-posts[4]) );
	Debug.Log("============================================");
	}
function fiveNonZeroForTen()
	{
	posts = new List.<int>();
	
	posts.Add(0);
	posts.Add(10);
	
	posts.Add( _unique0to10() );
	posts.Add( _unique0to10() );
	posts.Add( _unique0to10() );
	posts.Add( _unique0to10() );
	
	posts.Sort();
	
	print("the fenceposts are at...");
	Debug.Log( posts[0] +" "+ posts[1] +" "+ posts[2] +" "+ posts[3] +" "+ posts[4] +" "+ posts[5] );
	
	print("the five results are obviously...");
	Debug.Log(
	(posts[1]-posts[0]) +" "+
	(posts[2]-posts[1]) +" "+
	(posts[3]-posts[2]) +" "+
	(posts[4]-posts[3]) +" "+
	(posts[5]-posts[4]) );
	Debug.Log("============================================");
	}
function _unique0to10()
	{
	// NOTE normally DO NOT use merely logically determinate algorithms,
	// such as this one. but it's OK here.  because life's like that.
	var r:int=0;
	if ( posts.Count >= 11 ) return 0; // for safety, if you don't understand this line ignore it
	while ( posts.Contains(r) ) r = Random.Range(0,11);
	return r;
	}
function fiveNonZeroForTenShowHowPretty()
	{
	for ( var k:int=1;k<=1000;++k )
		{
		posts = new List.<int>();
	
		posts.Add(0);
		posts.Add(10);
	
		posts.Add( _unique0to10() );
		posts.Add( _unique0to10() );
		posts.Add( _unique0to10() );
		posts.Add( _unique0to10() );
	
		posts.Sort();
	
		Debug.Log( "   "+k+"    "+
		(posts[1]-posts[0]) +" "+
		(posts[2]-posts[1]) +" "+
		(posts[3]-posts[2]) +" "+
		(posts[4]-posts[3]) +" "+
		(posts[5]-posts[4]) );
		}
	}