Google Sheets: how to trigger random function? [duplicate] - if-statement

This question already has answers here:
How to stop / freeze / pause volatile RAND / RANDBETWEEN / RANDARRAY?
(2 answers)
Closed 2 years ago.
I have a sheet on Google and keep looking for a solution on how to make randomizing script work only when it's needed, not every time I open the sheet or refresh it.
Here's the example. Let's take the sheet looks like this:
B5 cell has the following code:
=index(B1:B3; randbetween(1;3); 1)
It randomly tells me the hair color every time I refresh or load the document.
But what if I want the new hair color only once a week, but need to open the document from time to time?
I have tried to put a flag to the code.
And changed the code of B5:
=if(B7=true; index(B1:B3; randbetween(1;3); 1); )
So it initiates randbetween function only when the flag is checked.
BUT! When the flag is unchecked, the hair color is shown empty (right, 'cause it has a space in IF function). How do I keep the hair color the same as it was before and make it change only if I check the flag?
Here's the spreadsheet.

in your B5 use:
=ARRAYFORMULA(IFNA(VLOOKUP(
IFNA(REGEXEXTRACT(TEXT(PI()^3*PRODUCT(LEN(IF(B7=TRUE;
{WHATTHEFOXSAY(); WHATTHEFOXSAY(); WHATTHEFOXSAY();
WHATTHEFOXSAY(); WHATTHEFOXSAY(); WHATTHEFOXSAY();
WHATTHEFOXSAY(); WHATTHEFOXSAY(); WHATTHEFOXSAY()};
{WHATTHEFOXSAY(); WHATTHEFOXSAY(); WHATTHEFOXSAY();
WHATTHEFOXSAY(); WHATTHEFOXSAY(); WHATTHEFOXSAY();
WHATTHEFOXSAY(); WHATTHEFOXSAY(); WHATTHEFOXSAY()})))*
(TRANSPOSE(SEQUENCE(1; 1; 29; 73)))*PI()^3; "0");
JOIN("|"; SORT({3;2;1}; 1; IFERROR(0/C2)))); 2);
{ROW(B1:B3)&""\ B1:B3}; 2; 0)))
and for weekly recalculation use this in C1:
=IF(IFERROR(WEEKDAY(TODAY(); 2); 0)=1; {"";1}; {"";0})
so the value of B5 will change every time you check / uncheck the checkbox and every понедельник (Monday)
more on WHATTHEFOXSAY function here

Maybe you just make a button for running a function that you will click once a week?
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('The name of menu')
.addItem('Button name', 'functionName').addToUi();
}

Related

How can I keep the ATR of a specific candlestick inside a variable without it changing with each new candlestick

Here is my code:
def TrueRange = ATR(14)[1];
On each new candlestick, thinkorswim generates a new ATR value and TrueRange fills with the ATR value of the previous candlestick.
I'm looking for a way to keep the ATR value of the first candlestick on the chart inside TrueRange without it changing with every new candlestick.
Can anyone help me?
You can use a recursive variable:
def TrueRange;
if BarNumber() == 1 {
TrueRange = ATR(14)[1];
} else {
TrueRange = TrueRange[1];
}
The first line declares the variable.
If you're at the first bar, set the variable to the value for that bar.
Otherwise, keep the variable at the value it was before.
Edit: you can also do this in one line:
def TrueRange = ( if BarNumber() == 1 then ATR(14)[1] else TrueRange[1] );
Edit 2: if you're using multiple offset/length values, thinkScript will override length/offset values on variables and plots to use the highest value present in the script, rather than what is specified. In that case, you would need to use CompoundValue to force the script to use the correct offset.
def TrueRange = CompoundValue(length=1, "visible data"=if BarNumber() == 1 then ATR(14)[1] else TrueRange[1], "historical data"=Double.NaN);
Reference my CompoundValue discussion at Understanding & Converting ThinkScripts CompoundValue Function

If the cell contains the word "xxx" then do "xxx"

I am working on a spreadsheet and I am trying to create a script, but I don't know how to do this:
I want to write an if statement where if the last row of column B contains the word, let's say "flower", then do something.
Can you please help me?
Thank you in advance.
Here is a working example of how you can use Google Scripts to do an if statement. Paste this into the script editor, then run it. If A1 has the value 1, then it will change B2 to "Yay!", otherwise B2 will be "Really?!"
function myFunction()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A1"); //gets a cell to test the value of
if (range.getValue == 1)
{
sheet.getRange("B2").setValue("Yay!") //sets the value of B2 if true
}
else
{
sheet.getRange("B2").setValue("Really?!") //sets the value of B2 if false
}
}
You can also add in else if if you want to add additional conditions, then whatever manipulation or function you want to do needs to inside the brackets

How would I randomly create typos in a std::string?

Ok so I'm working on a chatbot and I have a private std::string called m_sResponse. This string is outputted using
void print_response() const {
if(m_sResponse.length() > 0) {
std::cout << m_sResponse << std::endl;
}
}
I want to create a function that will misspell m_sRensponse let's say 5% of the time so the chatbot seams more human like. How would I accomplish this?
To make it seem more realistic, I'd make a map<char,vector<char>> of appropriate 'substitution' keys based off of keyboard layouts (e.g. QWERTY). Basically, it seems more real if your typo is "responsw" than "responsl" since "w" and "e" are next to each other. You'll also want to randomly delete or insert letters too. I'd assign a frequency to "errors" and then a frequency of each kind of error.
Now that you've got this and the other answers handling the randomness aspect (if(rand(100)<5)), you should be able to replicate the desired typo handler.
Pseudocode:
if rand(100) < 5
randomIndex = rand(string.length())
randomChar = rand(26)
string[randomIndex] = randomChar
You can use a random seed and use %5 for like 20% of the time ish.
if((rand() % 5) == 0) {
int t = rand() & m_sResponse.length();
char a = m_sResponse[t];
m_sResponse[t] = m_sResponse[t+1];
m_sResponse[t+1] = a;
}

How is it that lastTimeTargetAdded is ever not 0

Im beginning work with Cocos2d and have been working off this tutorial (link is for part 9) the past few days.
In reading the source and trying to understand it I have reached a section that doesnt make any sense to me.
-(void)gameLogic:(ccTime)dt {
static double lastTimeTargetAdded =0;
double now = [[NSDate date] timeIntervalSince1970];
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
if(lastTimeTargetAdded == 0 || now - lastTimeTargetAdded >= delegate.curLevel.spawnRate) {
[self addTarget];
lastTimeTargetAdded = now;
}
}
called via this:
[self schedule:#selector(gameLogic:) interval:0.2];
With the fact that lastTimeTargetAdded is created and set each time the function runs how is it ever not 0? And if thats the case what is the point of lastTimeTargetAdded = now? With the if statement being an OR (||) it never evaluates the other side of it so why is that even there?
I understand well enough what the function does just not so much how it does it. This method is suppose to spawn creeps based on their spawn rate. Making sure all the creeps in the wave arent just dumped on screen. And the method does do that well enough.
it is a static var ... the first statement sets to 0 only on the first invocation of gameLogic. On each subsequent invocation, lastTimeTargetAdded has the value that was set in the previous invocation.

Qt - Using QEasingCurve's output for something other than an Animation

I wonder could anyone be of help.
If for example I wanted to use the output of the QEasingcurve for another purpose other than driving a QAnimation, is this possible? For example, if I had a numerical read out on my GUI that I wanted to grow, overshoot and bounce back into place could I use the QEasingcurve for this purpose?
I have used Qt for quite a while but have never dabbled with any of these parts of it - I am trying to work it out but cannot, so thought I'd ask here.
I am not sure I understand correctly what you want to display, but from what I understand, using QPropertyAnimation is probably the way to go.
However, to answer your question, you can of course use QEasingCurve in a standalone manner, you just need to use the valueForProgress(qreal progress) member function.
Hey - Just wanted to update with how I carried this out in case anyone looks it up in the future.
void RPM::updateGauge(float speed)
{
easing = new QEasingCurve(QEasingCurve::OutElastic);
easing->setAmplitude(1.0);
currentPosition = (float)ui->svgdial_currentSpeed->value();
newPosition = speed;
difference = newPosition - currentPosition;
interval = 0.0;
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(doGaugeMovement()));
timer->start(60);
}
void RPM::doGaugeMovement()
{
interval+=0.01;
ui->svgdial_currentSpeed->setValue(currentPosition + ( difference * easing-
>valueForProgress( interval ) ) );
if(interval >= 1.0)
{
timer->stop();
}
}
Simply used a timer to update the gauge slowly, pulling the valueForProgress result for its new position each time.