This question already has answers here:
Generating non-repeating random numbers
(5 answers)
Closed 9 years ago.
I use the following code to hide an UIButton from my rand_btns NSMutableArray.
int random = arc4random_uniform ([rand_btns count]);
if (random != level - 1) {
[[rand_btns objectAtIndex:random] setHidden:YES];
} else {
// call again this method
}
It works great, but ... it repeats. How can I stop repeating OR to check if an UIButton from my rand_btns array was hidden, don't need to include this objectAtIndex again.
I mean, if using my code, I can hide random button from my array excepting a specific objectAtIndex.
I would like to NOT REPEAT AGAIN THAN NUMBER from int random
You can refer to this Non-repeating arc4random_uniform. If you want to check if your button is already hidden or not, you can check button.hidden property.
If you want a random-ish sequence of numbers that doesn't repeat, one way to do it is to fill an array with consecutive numbers and then shuffle the list so that the numbers are ordered randomly. That way, you know that each number occurs only once, but you can't predict the order that the numbers appear in.
Random numbers can repeat, since they are random. You probably need to hold a list of the random numbers you have been served so far, and when you want a new number keep trying until you get a number that's not in your list. Also beware that if you're looking for a number that you haven't had so far then sooner or later you're going to run into an infinite loop as eventually there will be no more "free" numbers in the range you want
Related
I'm trying to make somethin like a Chutes & Ladders game in excel. Meaning that i automatically generate a number from 1-6 like a dice roll and then i want to make a system, which will sum all of these rolls (all are saved in individual cells) and determine where should the player be standing on cells 1-100.
I made a cell which sums all of the rolls and then a SWITCH function, which moves the result number if it detects a number that is either a chute or a ladder and outputs the resulting number.
The problem is that lets say that player lands on cell "2", there a ladder there and leads to cell "21". If the switch detects 2, it correctly says 21. But then the player rolls the dice, lets say 4 and it outputs 6, because its just 2+4=6 and doesnt remember the previous 21. The correct answer should be 27. Is there any way to make it work?
Create a public variable to hold the previous "location".
Public accumulator
Then
accumulator=accumulator+diceroll1+diceroll2
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
I am trying to find the average of the seven most recent entries in a row, as seen in
this
spreadsheet.
I found a few questions similar to mine, but I am still pretty confused on how the answers work. The questions similar to mine can be found on the left side of my spreadsheet.
I think that the formulas would work for me with a few simple adjustments of which values to use, but I can't seem to figure it out. I would really appreciate if someone could explain one of the existing answers or come up with another one that works.
The spreadsheet is updated daily, so I need something that would continue to work as more and more data is added to the column.
Try:
=round(AVERAGE(OFFSET(H1,MAX(ARRAYFORMULA(ROW(H:H)*--(H:H<>"")))-7,,7)))
here's working sample
Explanation
We are getting the last non empty row: MAX(ARRAYFORMULA(ROW(H:H)*--(H:H<>"")))
Then with offset formula we are getting the range of last 7 cells in a column.
And then just use AVERAGE.
More info
You may find more info about finding the last non empty row here:
Selecting the last value of a column
Another way is to use INDEX and MATCH. The first match finds the position of the last number in the range and takes 6 away from it: the second match finds the position of the last number in the range. Passing it through the INDEX function gives a reference that you can use to give a range of 7 cells for AVERAGE to work on.
=average(index(H:H,match(999,H:H)-6):index(H:H,match(999,H:H)))
So my answer is like your Link2
The big snag here is if you have a text cell in the range (like "Nothing") it is much more difficult to work out which cell to start from to get an average of 7 cells. I think I know how to do it in Excel using OFFSET but offset doesn't seem to work in the same way in Google Sheets.
However I can see there is a solution to this in your Link3 which should work for you if you change A:A to H:H and SUM to AVERAGE. I have tested it on the average of the last ten cells which includes a "Nothing" cell:
=ArrayFormula(AVERAGE(QUERY(SORT(H:H,ROW(H:H)*ISNUMBER(H:H),0),"select * limit 10")))
and it gives the correct answer 61.8.
The way array formulas work in general is that instead of passing a single value to a function you pass a whole range or array (a list of values) and the function processes them one by one. The above formula takes the whole column H:H and sorts it on the row numbers in descending order but those cells which don't contain a number give zero in the multiplication and are sorted to the bottom. Then the query takes the top (in my case) 10 cells and passes them to AVERAGE.
BTW this doesn't have to be declared as an array formula: this also works
=AVERAGE(QUERY(SORT(H:H,ROW(H:H)*ISNUMBER(H:H),0),"select * limit 10"))
I am using the Xcode 7 beta 5 and Swift 2. I currently have a UITextField and a UIButton. The UITextField is set to a decimal pad, but however, the user is able to enter in multiple decimals. That is not how I would like the app to work. I would like to check, when the button is tapped, if the text field has 0-1 decimal points. Is there any statement that checks for multiple decimal points?
Replace textField with a reference to your UITextField in this:
if textField.text.componentsSeperatedByString(".").count >= 3 {
// more than 1 decimal point
}
This seperates the text in the text field into an array and creates an array of strings separated on any decimal points. If there are 0-1 decimal points, the array will have less than 3 items. This documentation might be helpful.
Give the text field a delegate and implement textField:shouldChangeCharactersInRange:replacementString: to prevent the user from entering a decimal point if there is already one present.
I HAVE THIS: A tableview with cells with a label were user gets a number. (From core data from a detail view).
WHAT I WANT: How to make an operation with all the cells with a number on a label on that cell.
Example, sum up all the cells label. label at cell 1, + label at cell 2, and so on.
MY PROBLEM: i can do it but with a lot of code, i have to make an operation for each cell user may create. (Maybe user creates 1 or 20 cells), and i think to make an operation with the index path and value for 20 cells its not necessary.
Im sure its another way but i don't know how.
Thanks from know to everybody.
So, you have a couple of options, this one is little code but isn't so efficient as the numbers start to add up:
NSArray *myNumbers = ...;
I'm assuming that this array exists and is being used to drive your table view. It contains the instances of your Numbers entity. Based on that, you can get the sum:
NSNumber *sum = [myNumbers valueForKeyPath:#"#sum.mynumber"];
The alternate route is to use NSExpression and NSFetchRequest to run the summing operation on the entities in the data store without actually reading everything into memory like the array does.
There is also a middle ground option where you could just run an NSFetchRequest to get only the mynumber values in dictionaries and then use valueForKeyPath:. The value of this option depends on how many attributes your Numbers entity has and what they contain.