Seeding a 2d array with x many elements.

BlitzMax Forums/BlitzMax Beginners Area/Seeding a 2d array with x many elements.

Ryan Burnside(Posted 2008) [#1]
This problem is frustrating me. I don't quite know how to tackle this elegantly.

Basically I need to fill an array that is an 4x4 array. The array should have two possible values for the value of each index. I would like to fill this 4x4 array with each index either holding value "a" or value "b". Also I need to be able to specify the percent of indexes that have a value of "b".

So given a percent for the number of elements with the value "b" create a 4x4 array and insert the percent of B values completely at random.


So for example if you wanted 50% of the elements to contain the value "b" you might have the following:

a a a b
b b a a
a b a a
b b b b

The percent must also be rounded to fit whole numbers of elements containing the value "b" into the array.

Constraints
1. Use random distribution of value "a" and "b" without extra "guess and check" index picking cycles.

2. Given a percent of elements containing "b" fill the array with that many elements rounding the percent if necessary, the other elements contain value "a"


Czar Flavius(Posted 2008) [#2]
Create a results array of a size equal to the number of elements in the main 4x4 array. Fill up with the right amount of b and the rest with a. This should be easy.

Look up a shuffling algorithm (or see if Blitz has a built-in one) and apply it. You now how an array with just the right number of elements contiaining the right number of b in a random order.

Loop through the whole 4x4 array and one-by-one plonk each element from the results array in. :)


Ryan Burnside(Posted 2008) [#3]
The problem is getting there.

How do I fill a 2d array with the correct number of elements for shuffling?


Bremer(Posted 2008) [#4]
This is probably not the best solution, but just something I made in 10 minutes.

SeedRnd MilliSecs()

Local i:Int,ii:Int,p:Int
Local s:String
Local array:Int[4,4]

Local percent:Int = Rnd(100)
Local b_amount:Int = 16*percent/100
Local a_amount:Int = 16-b_amount

Print "Percent of B: "+percent
Print "Amount of B's: "+b_amount
Print "Amount of A's: "+a_amount
Print "Total: "+(b_amount+a_amount)


For i = 1 To b_amount
	Repeat
		px = Rnd(4)
		py = Rnd(4)
	Until array[px,py] = 0
	array[px,py] = 1
Next
For i = 1 To a_amount
	Repeat
		px = Rnd(4)
		py = Rnd(4)
	Until array[px,py] = 0
	array[px,py] = 2
Next

For i = 0 To 3
s = ""
For ii = 0 To 3
	s :+ array[i,ii]+","
Next
Print s
Next



Ryan Burnside(Posted 2008) [#5]
Thanks! That works well. ;)


Czar Flavius(Posted 2008) [#6]
How do I fill a 2d array with the correct number of elements for shuffling?


I don't think you understood what I meant.

Your 4x4 array contains 16 elements. Say 25% of them are required to be b.

Create a new, seperate 1d array of 16 elements. Fill the first 4 of these with b (25%) and fill the remaining with a.

You now have an array of 4 b and 12 a. Use a shuffling algorithm on the array.

You now have a single array of 16 elements: 4 b and 12 a, but in a random order.

Now loop through your 2d array like this:
Local i:Int = 0
For Local x:Int = 0 Until 4
	For Local y:Int = 0 Until 4
		array2d[x, y] = shuffledarray[i]
		i = i + 1
	Next
Next