14
3
Two random numbers A and B have been generated to be either 1, 2, or 3
your job is to randomly pick a third number C that can also be 1,2 or 3. But, C cannot equal A or B.
- And Yes, A can equal B.
- If A = B, then C has only two numbers left it can be.
- If A does not equal B, C has only one number it can be.
- Assume A and B have already been chosen for you
This is how A and B would be created in Python
A = random.randrange(1,4)
B = random.randrange(1,4)
Assume this is already in your code.
This is the shortest I've come up with in Python
while True:
C = random.randrange(1,4)
if C != A and C != B:
break
This is what A, B and C can equal.
- 1,2,3
- 1,1,2
- 2,3,1
- 3,3,2
This is what A, B and C can't equal
- 1,2,1
- 2,3,3
- 1,1,1
- 3,2,3
1So if A and B are given, they aren't actually random as far as my program/function is concerned, right? Also what do you mean by "general code"? Are you actually looking for a solution you can use in a project of yours? In that case, I wouldn't look to code-golf for help - the code is going to be absolutely unusable in production. The entire point of code-golf is to abuse language-specific features to get the code size as far down as possible. – Martin Ender – 2014-07-04T17:56:03.413
I was using this for a project and have already got a long answer, but I am asking this because I thought it was interesting problem that the community would enjoy. And I put general code because I find it more interesting when someone used more logical thinking to get an answer rather than vast knowledge of some language-specific features, but if code-golf is to abuse these, then I will remove that last line. – tysonsmiths – 2014-07-04T18:00:37.407
Well assume A and B can equal either 1, 2 or 3, so right your code to anticipate all of the possibilities. – tysonsmiths – 2014-07-04T18:02:23.890
1I don't understand. Is there something I'm missing or does
C = (A!=B ? 6-A-B : (!(A-1) ? 2 : 1))
work? Also your solution is highly inefficient as it wastes time looping and possibly could take an infinite amount of time to run. Also,import random
counts in byte size... – DankMemes – 2014-07-04T18:14:39.020I edited my question to add clarity – tysonsmiths – 2014-07-04T18:18:33.173
You can do
random.randint(1,3)
to make it shorter. – user80551 – 2014-07-04T18:20:17.353If
A,B=random.randrange(1,4),random.randrange(1,4)
is already in the code, can I assume thatimport random
is also there or will it count towards my byte-count? – user80551 – 2014-07-04T18:23:21.213You can assume that's already in there. Assume A and B are variables already created. – tysonsmiths – 2014-07-04T18:25:06.723
Whoops I made a mistake in my above comment. That code should be
C=A!=B?6-A-B:!(A-1)?2:!(A-2)?3:2
(32 bytes by the way) – DankMemes – 2014-07-04T18:40:02.9903
So you want us to write a Monty Hall simulator? ;-)
– Ilmari Karonen – 2014-07-05T02:01:17.317@IlmariKaronen I totally missed the similarity to Monty Hall! it is indeed equivalent to the choice the host makes once he has the player's first guess (since he already knows where the prize is.) However it doesn't simulate a) the placement of the prize or b) the choices made by the player. I don't think this is a duplicate of the other Monty Hall questions. – Level River St – 2014-07-05T10:05:29.647
Does it have to be a complete program or is a snippet enough? (e.g. leaving out
main()
in C) – nyuszika7h – 2014-07-06T14:12:35.540