C++ define function syntax with rand - c++

I am working on a class assignment with starter code and included is this syntax that I've never seen before.
#define EVENT() (rand() % 2 > 0.5)
Can someone tell me what this does? Does it return something? I know the define is a macro that has a function called EVENT(), but what does the (rand() % 2 > 0.5) bit mean? If I use EVENT() in the code somewhere, do I treat it as a void function that just evaluates to that statement?

wherever EVENT() is used in the source code , the preprocessor substitutes EVENT() with rand() % 2 > 0.5 which will return a random 1 or 0, 1 if the rand() result was odd and 0 if the result was even.

Related

How can I write Collatz conjecture by SymPy?

I was going to write a SymPy function which takes a natural number and returns the step number of Collatz conjecture of the arg. The Python version is here:
def collatz(n: int):
step = 0
while n != 1:
n = n // 2 if n % 2 == 0 else 3 * n + 1
step += 1
return step
print(collatz(27))
It prints 111.
How about SymPy version? I feel sympy.Lambda() should have a recursive feature, similar to recursive call of procedual programming. Is there a good way?
Just calling collatz() with a sympy.Symbol() instance (obviously) went into iloop.
sympy.series.sequences.RecursiveSeq() does only backward reference with constant decrements.
Let f(x) be the symbolic collatz result. Do a substitution followed by a replacement to replace any non-symbolic results:
>>> f=Function('f')
>>> eq = f(x)
>>> eq.subs(x, 270)
f(270)
>>> _.replace(lambda x: x.func == f and x.args[0].is_Integer, lambda x: collatz(x.args[0]))
42
If you don't want to do it like this and want automatic evaluation, then you will have to write a SymPy class deriving from Function that has an eval method that detects when the input is an Integer. You can look at any function to see how this is implemented, e.g. see the source code for cos.

About random numbers in C++

I am really new to C++. I am following a free online course, and one thing I had to do was to create a program which could scramble the characters of a string.
So, I created a function who received the word as parameter and returned the scrambled word. ctime and cstdlib were included and srand(time(0)); declared in the main.
Basically, the function looked like this :
std::string mixingWord(std::string baseWord)
{
std::string mixWord;
int pos(0);
for (int i = baseWord.length; i >= 0; i--)
{
if (i != 0)
{
pos = rand() % i;
mixWord += baseWord[pos];
baseWord.erase(pos,1);
}
else
{
mixWord += baseWord[0];
}
}
return mixWord;
}
And it worked just fine. But the correct solution was
std::string mixingWord(std::string baseWord)
{
std::string mixWord;
int pos(0);
while (baseWord.size() != 0)
{
pos = rand() % baseWord.size();
mixWord += baseWord[pos];
baseWord.erase(pos, 1);
}
return mixWord;
}
And it works fine as well.
My question is :
Why is the solution working ?
From what I understood, this :
rand() % value
gives out a value between 0 and the value given.
SO, since baseWord.size() returns, let's say 5 in the event of a word like HELLO. rand will generate a number between 0 and 5. So it COULD be 5. and baseWord[5] is out of bound, so it should crash once in a while, but I tried it over 9000 times (sorry, dbz reference), and it never crashed.
Am I just unlucky, or am I not understanding something ?
x % y gives the remainder of x / y. The result can never be y, because if it was, then that would mean y could go into x one more time, and the remainder would actually be zero, because y divides x evenly. So to answer your question:
Am I just unlucky, or am I not understanding something ?
You're misunderstanding something. rand() % value gives a result in the range [0,value - 1] (assuming value is positive), not [0, value].
rand() % 100 returns number between 0 and 99. This is 100 NUMBERs but includes 0 and does not include 100.
A good way to think about this is a random number (1000) % 100 = 0. If I mod a random number with the number N then there is no way to get the number N back.
Along those lines
pos = rand() % baseWord.size();
will never return pos = baseWord.size() so in your case there will not be an indexing issue
I guess you just misunderstood the modulo operator. a % b, with a and b any integer, will return values between 0 and b-1 (inclusive).
As for your HELLO example, it will only return values between 0 and 4, therefore will never encounter out of bound error.

qrand is returning always 0

I need to get random numbers between 0 and 1.
As 0.54321, 0.8912, 0.1234342, 0.0000123 and etc
I put this code in my main and also Application constructor:
qsrand(QDateTime::currentDateTime().toTime_t());
And used this code inside one of my slots:
float prob = qrand() % 1;
I tried int, double as a return value, but it is always returning 0.
Any ideas what is going on?
Thanks
qrand() generates integer numbers between 0 to RAND_MAX and every number is perfectly divisible by 1 and giving remainder as 0. Try this instead:
float prob = (float) qrand() / (RAND_MAX+1); // 1 is exclusive

min and max simply doesn't work in this case?

So here's the code which works. Posted it without any changes.
There is X and Y values which must be betwen 0 and 1024. tyleSize is 1024;
//for X
int loffx=accurate(curphisobj->x) + (rand() % 100) - 50; //some math, doent matter
loffx=max(loffx,1);
loffx=min(loffx,tyleSize);
//for Y
int loffy=accurate(curphisobj->y) + (rand() % 100) - 50;
loffy=max(loffy,1);
loffy=min(loffy,tyleSize-3);
But if I write it like this:
int loffy=min(max(accurate(curphisobj->y) + (rand() % 100) - 50,1),tyleSize - 2);
int loffx=min(max(accurate(curphisobj->x) + (rand() % 100) - 50,0),tyleSize);
I get loffx and loffy 1034, 1029, -5, - 2, all kinds of nombers uncut by max and min.
Is there something i dont know about C++ compiler, or there's some dumb mistake?
Make sure that you're actually using the min and max functions from <algorithm>, and not some macros defined elsewhere. For instance, the Windows header windef.h defines max like this:
#define max(a,b) (((a) > (b)) ? (a) : (b))
This won't work in your code because it potentially evaluates each argument twice, and rand by design returns a different result each time.
Try viewing the source after preprocessing to see if you're using the macros. You can turn off the Windows macros by defining NOMINMAX before including any headers.

C++ inline if fails in switch

I just found out the hard way an inline if (A?B:C) does not work as expected in a switch statement.
where A a boolean, B and C both integer unequal to 0. The result of this statement is 0 when placed inside a switch.
I found a stackoverflow post [1] where this behaviour was mentioned but I can not find any explanation why this doesn't work as I would expect. What is causing this?
For example:
int foo = 6;
switch(foo)
{
case 6:
return 10 + true ? 2 : 4;
}
[1] Benefits of inline functions in C++?
This is nothing to do with switch.
10 + true ? 2 : 4
is equivalent to:
(10 + true) ? 2 : 4.
If you want it to act like:
10 + (true ? 2 : 4)
then you will need to write it like that.