How to generate a certain amount of numbers and spread them randomly across a grid? - delphi

I want to generate the number 2 5 times and the number 1 10 times. I'm trying to spread these across a String Grid in Delphi randomly. I also want to fill the rest of the grid that isn't 1 or 2, with 0's. I have no idea how to even start here.
It would look something like this (P stands for player and there would only be 5 2's and 10 1's): https://gyazo.com/aeef05c3a92ce7847c0f42ad40faa733

Given a grid with dimensions m×n, create an array of length m * n. Put five 2's and 10 1's in the array, and fill the remainder with 0's. (We'll assume the product of m and n is at least 15.) Shuffle the array. Copy each element of the shuffled array into successive cells in the grid.

While the approach represented in Robs answer will do the job I personally think it is way to complicated for it's purpose.
So what would be more simpler approach?
Well your goal is to place these numbers at random positions in grid.
How do you determine position of some object in a grid? You do it by its X (Column) and Y (Row) coordinates.
So how do you get random position in a grid? Simple chose two random values for X and Y coordinates.
As for placing certain numbers of number 1 and number 2 use two simple loops.

Related

How do I create a list of non-repeating cells/numbers in Google Sheets?

I’m trying to emulate Minesweeper in Google Sheets, and for this I want to create a second map adjacent to the first with all of the correct values already in it. To randomize bomb position, I need a list of random numbers or cells(cells would be preferable). However, I cannot figure out how to do this without ending up repeating numbers. The result would ideally be a vertical array of cell coordinates. Thank you!
Answer
The following formula should produce the result you desire:
=SORTN(FLATTEN(MAKEARRAY(10,10,LAMBDA(row,col,ADDRESS(row,col)))),20,,RANDARRAY(100),)
In =MAKEARRAY, change the first 10 to adjust how many rows to randomly choose from, or the second 10 to adjust how many columns to choose from. The value in =RANDARRAY must be equal to the product of the number of rows and the number of columns. (e.g. in the above example, 10*10=100).
Change the 20 to adjust how many randomly chosen values to return.
Explanation
=MAKEARRAY is used to generate an array of every possible row and column combination. It accepts a =LAMBDA, which in this case is just the =ADDRESS function. The first two arguments of =MAKEARRAY determine how large the array should be, which is why changing them adjusts how many rows/columns to randomly pick from.
Then, the result of =MAKEARRAY is squashed into a single column using the =FLATTEN formula.
Finally, the entire thing is sorted randomly using =SORTN combined with =RANDARRAY. =SORTN also limits the number of results that are returned dependent on its second argument, which is why changing it adjusts how many results are returned.
If you want information on how to "freeze" the value of =RANDARRAY so it doesn't recalculate each time you change something, check out this question by player0.
Functions used:
=MAKEARRAY
=LAMBDA
=ADDRESS
=FLATTEN
=SORTN
=RANDARRAY

Random select in with a bias towards certain outcomes (ie 60/40)

Lets say I have 2 lists and I would like to randomly select a winner between the lists but I would like to select the winner from list A 60% of the time and from list B 40% of the time, how can that be done in Google Sheets?
You can randomly select names from a list using this formula
INDEX(A2:A, RANDBETWEEN(1, COUNTA(A2:A)))
Without knowing some more information on your setup here is a general formula that does what you're describing:
=IF(RAND()<=0.6,INDEX(A2:A, RANDBETWEEN(1, COUNTA(A2:A))),INDEX(B2:B, RANDBETWEEN(1, COUNTA(B2:B))))
Essentially it is rolling a random number between 0 and 1. If it is equal to or less than .6 (simulating 60%, since there is a 60% chance it will be less than or equal to .6) it then selects a random name from Column A, otherwise (bottom 40%) it selects from column B.
You can also replace the "0.6" with A1 in my example to have the weight be a dynamic number. Changing A1 to 75% for example will then compare the random value against less than or equal to .75.
EDIT: Image shows the wrong condition, I was corrected in the sense you want less than or equal to .6 and not greater than, I had the weights flipped.

How to calculate Dimensions from a Text String in a single cell?

I am need to calculate the dimensions from cell values that are entered as strings into a single cell vs. 3 separate cells and I do not want to break the dimensions into Length (L) x Width (W) x Height (H) Columns.. instead I am hoping there is a relatively simple function that would allow me to calculate the total cubic dimensions from that single cell.
I am aware of this tutorial that can take a string and be used to break it into 3 separate cells.. but that defeats the point of what I am trying to do.
My data looks like this:
Dimensions
Cub/In.
CF
70x13x13
11830
6.85
24*18*13
5616
3.25
24x16x12
16x24x10
Right now the data is entered as either "LxWxH" or "L*W*H" in that text formant and the columns that have values like the 5616 above are me manually re-entering "=24*18*13".. literally one character difference.
I did try a CONCATENATE to just append an "=" to the beginning but got errors on all in Google Sheets (for comparison) or a Literal string into processed as a formula in Excel.
=CONCATENATE(“=”,B1)
Looking for a simple way to do this calculation in a single column and being able to have to enter the data once or utilize the existing data. I don't mind doing a single bulk replace of "x" to "*" on the input column to standardize the source column but don't want to have to do a series of bulk replaces every time I want to run through the data.
Thoughts?
Use SUBSTITUTE to get them all to the same, then use SPLIT and wrap in PRODUCT:
=PRODUCT(SPLIT(SUBSTITUTE(A2,"*","x"),"x"))
Or shorter Version shown by #JvdV:
=PRODUCT(SPLIT(A2,"*x"))

How to make scatter plot in google sheets using 2 columns as X and Y values and the 3rd columns to get color of that point?

I have 3 columns in a google spreadsheet
Runs Balls Result
24 24 W
50 20 W
10 5 L
12 10 L
5 2 L
Now I want a scatter plot to be made, such that Runs and Balls are the X and Y axes, and the color of the point is determined by the values in the column Result.
How do I achieve this?
You could separate your second column (y-value, "Balls") into two separate columns, depending on if the Result is W or L. In other words, make a chart based on three columns instead of two. This way, two different colors will be assigned automatically when the chart is generated. (Later, you can change these colors by double-clicking on the chart to get to Chart Editor -> Customize -> Series -> Format -> Color).
There seem to be no good solutions here. Answer from Monaco works but is inconvenient in cases. I settled for adding the third column as a label for the second one.

Google Sheets - Multiply field by field three to the left

My title might not be very specific, so I'm going to try to explain a little better.
The sheet is divided into a name(Column A), containing a certain number of values(Column B), that get added together to a total in Column C. Furthermore, Column D, E and F contains the values I want the Total in Column C multiplied by. These first columns A to F I just fill in manually, but I would like a function to calculate the Columns I've called x, y and z total (G, H and I).
I see a pattern in this, I just can't figure out the syntax to get Sheets to see it aswell.
The pattern I'm invisioning is for each row, I want column G, H and I to take the value 3 fields to their left, and multiply it by Column C, at their row number.
Is this somehow achievable? I tried finding a solution online but I guess I don't know how to word myself.
Here's a picture to maybe make everything a little clearer
This would save me alot of time, given that I have over a hundred different rows this calculation needs to be performed on...
If something is not clear, please feel free to write a comment. I'll be following this thread quite liberally.
Thanks in advance!
You can have this formula on the first xTotal:
cell G2: =ARRAYFORMULA(if(len(C2:C),C2:C*D2:D,))
cell G2: =ARRAYFORMULA(if(len(C2:C),C2:C*E2:E,))
cell G2: =ARRAYFORMULA(if(len(C2:C),C2:C*F2:F,))
I created a sheet with the same results you had before, but this time you don't need vertical columns, just say in the # of Values column how many numbers you should have below. You just need to input the values with the grey columns.
Note: This is assuming you will always have growing vertical numbers like 1,2,3,4,5. In the new sheet you just need to set 5 in the column and it will calculate the result.
Please make a copy of this sheet and edit as you wish.
Sheet
You can use a single, simpler formula for this in cell G2
=ARRAYFORMULA(if(C2:C="",,C2:C*{D2:D,E2:E,F2:F}))

Resources