Login Register






C++ Challenge - Randomization filter_list
Author
Message
C++ Challenge - Randomization #1
Objective: Create a condition assignable to a boolean variable, using a random generated number.

Constraints:
1. No use of division or modulus operations by any means
2. Can only use one call to a random generator function
3. No other hardcoded values, the only value you can use is the one generated by the random method itself, and the only other variable in your program should be a single boolean variable
4. Booleans true/false formed by values 0 or 1 are not allowed either from the randomly generated value
5. (No inline ASM)

Code:
#include <iostream> #include <cstdlib> #include <ctime> int main() { srand(time(0)); short/int/etc... r = { random generated value }; bool b = ___________; std::cout << (b ? "TRUE" : "FALSE") << std::endl; }
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: C++ Challenge - Randomization #2
To answer a question that was brought up. No, you don't have to create your own random number generator, but regardless of what method you choose, you can only have/use one randomly generated value, and you must satisfy the constraints listed in the first post, while being able to assign variable 'b', a boolean that will not always be the same value, it must be randomly assigned as true or false, but you can only use the one randomly generated integer to do so. As mentioned, no hardcoded values, etc...

Surprising that nobody has tried this yet, it's easy, it's just a way of thinking...
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: C++ Challenge - Randomization #3
(02-19-2014, 08:50 AM)Chav Wrote: Can i post solution?

What's the point in asking whether you can or not? :S
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: C++ Challenge - Randomization #4
(02-19-2014, 08:50 AM)Chav Wrote: Can i post solution?

What's the point in asking whether you can or not? :S
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: C++ Challenge - Randomization #5
Lololololo. Here's my quick super inefficient solution:

Code:
#include <iostream> #include <cstdlib> #include <ctime> int main() { srand(time(NULL)); int randInt; bool randBool; randInt = rand(); randBool = randInt & randInt * randInt & randInt * randInt * randInt & randInt * randInt * randInt * randInt & randInt * randInt * randInt * randInt * randInt & randInt * randInt * randInt * randInt * randInt * randInt & randInt * randInt * randInt * randInt * randInt * randInt * randInt & randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt & randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt & randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt & randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt & randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt & randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt * randInt; std::cout << (randBool ? "TRUE" : "FALSE") << std::endl; return 0; }

I tested it 100 billion times to see how often TRUE and FALSE occurred, I got on average that both TRUE and FALSE occurred 50% of the time. I could test to see how uniformly distributed sequences of numbers are (000, 001, 010, 011, 100, 101, 110, 111...) to see how random it really is but meh.. I didn't feel like it lol.

Reply

RE: C++ Challenge - Randomization #6
I couldn't think up a good solution, I just played around with the code. However, this works, had no idea how it does. I even tried studying about this, but no luck.

You should discuss about this.
Code:
#include <iostream> #include <cstdlib> #include <ctime> int main() { srand(time(0)); int r = rand(); bool b = r<<r<<r; std::cout << (b ? "TRUE" : "FALSE") << std::endl; }

Reply

RE: C++ Challenge - Randomization #7
Here was my solution:
Code:
bool b = r & (bool)r;

Because I'm not limiting the range for the value returned by rand() there's a high possibility that you'll get a result greater than 0. Casting the value to a boolean will result in 1. When you bitwise AND 1 to a value, you'll get a 1 if the numeric value is odd, and 0 if it is not. Effectively, this makes up for a pretty good 50:50 split for true/false.

All the other methods won't be as fair/non-biased from what I'm seeing.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply