随机选择一个与其他两个随机数不同的数字


14

已生成两个随机数A和B分别为1、2或3

您的工作是随机选择第三个数字C,该数字也可以是1,2或3。但是,C不能等于A或B。

  • 是的,A可以等于B。
  • 如果A = B,则C仅剩两个数字。
  • 如果A不等于B,则C只能是一个数字。
  • 假设已经为您选择了A和B

这就是在Python中创建A和B的方式

A = random.randrange(1,4)
B = random.randrange(1,4)

假设这已经在您的代码中。

这是我在Python中想到的最短的

while True:
    C = random.randrange(1,4)
    if C != A and C != B:
        break

这就是A,B和C可以相等的。

  • 1,2,3
  • 1,1,2
  • 2,3,1
  • 3,3,2

这是A,B和C不能相等的

  • 1,2,1
  • 2,3,3
  • 1,1,1
  • 3,2,3

1
因此,如果给定A和B,就我的程序/功能而言,它们实际上并不是随机的,对吗?另外,“通用代码”是什么意思?您是否正在寻找可以在您的项目中使用的解决方案?在这种情况下,我不会向代码高尔夫球寻求帮助-代码在生产中将绝对无法使用。代码高尔夫球的重点是滥用特定于语言的功能,以使代码大小尽可能地小。
— Martin Ender 2014年

我将其用于一个项目,并且已经得到了很长的答案,但是我之所以这样问,是因为我认为社区会喜欢这个有趣的问题。我之所以放上通用代码,是因为当人们使用更多的逻辑思维来获得答案而不是对某些特定于语言的功能的广泛知识时,我发现它更有趣,但是如果代码高尔夫球滥用了这些功能,那么我将删除最后一行。
— tysonsmiths 2014年

好吧,假设A和B可以等于1、2或3,因此正确编写代码以预期所有可能性。
— tysonsmiths

1
我不明白 有什么我想念的东西C = (A!=B ? 6-A-B : (!(A-1) ? 2 : 1))吗?此外,您的解决方案效率极低,因为它浪费了时间循环,并且可能需要花费无限长的时间才能运行。此外,import random字节数也很重要...
— DankMemes

3
所以,您要我们编写Monty Hall模拟器吗?;-)
— Ilmari Karonen 2014年

Answers:


17

Ruby,22个字符

([1,2,3]-[A,B]).sample

仍然不确定我是否正确理解了这个问题...


12

C,26

a-b?6-a-b:(rand()%2+a)%3+1

如果我正确理解了这个问题:

如果a和b不同,则没有随机性。答案必须是未使用的1,2,3中的唯一一个:6-a-b。

如果a和b相同,则有2个选择:

a=b=           1 2 3
            return value    
rand()%2=0     2 3 1
rand()%2=1     3 1 2

5

Befunge(156 89 85 74)

好的,这很糟糕,我知道。但这是我第一次尝试Befunge,所以我仍然很高兴它能起作用。我敢肯定会有更好的解决方案。

<v1p90&p80&
<<@.g70_v#-g70_v#-g70g90g80p70
  v     <      <
^1?v
^3<2
^  <

1
现在在我的手机上。。不确定某些行是否意外移位。待会我会检查的。
— IngoBürk'14

在我看来很好。比我的99字节尝试要好得多。希望您在玩趣味游戏。
— AndoDaan 2014年

是的,移动版本似乎不是等宽的。Befunge是很有趣的,但我希望有会是一个不错的Befunge-98的解释(我无法找到任何... /编辑:rcfunge似乎工作)
— 英戈·伯克

3

GolfScript,13个字符

~0]4,^.,rand=

这是一个完整的GolfScript程序,它从标准输入中读取两个以空格分隔的数字(每个数字假定为1、2或3),并从集合{1、2、3}中输出一个不等于任何数字的随机数输入数字。

在线尝试。(请注意:链接是以前的版本;我在移动设备上,无法修复。)

这是该程序的注释版本:

~         # eval the input, pushing the input numbers onto the stack
0         # push the number 0 onto the stack
]         # collect all the numbers on the stack into an array
4,        # create another array containing the numbers 0, 1, 2 and 3
^         # xor the arrays (i.e. take their symmetric set difference)
.,rand=   # choose a random element from the array

如果您希望使用一个将两个数字作为堆栈参数的命名函数,则需要再添加几个字符:

{[\0]4,^.,rand=}:f;

该函数的实际主体仅比独立代码长一个字符(因为我们需要[确保仅使用两个参数),但是将代码包装在一个块中并将其分配给一个符号的开销为五个更多字符,总计19个。

另外,如果您确实在变量A和中分配了两个数字B,并希望将第三个数字分配给C,则也可以用19个字符完成:

4,[0A B]^.,rand=:C;

(如果可以将第三个数字保留在堆栈中,则可以将 :C;不加结尾。)

附言 感谢您使用^霍华德的建议。


代替\-您可以使用^。
— 霍华德


2

Python,14个字符

我每9个可能的情况下都尝试过一次,看来效果不错!

C=A^B or A^1|2

(编辑):正如edc65所指出的,这是无效的,因为它不是随机的。


3
一点也不随机
— edc65

不过,录取还是+1
— Lister先生2014年

2

Befunge-99个字节

&:01p&:11p-!!#v_v
   @,g2+g11g10< "
   321 vv*2g<"v ^
 2v v v 5v*2^10<"
 v?v?v?vp5     ^<
 2 3 1 2<        
 > > > >.@       

不太令人印象深刻。


2

PowerShell,21岁

1..3-ne$A-ne$B|random

非常简单。滥用比较运算符将数组作为其左操作数的行为有所不同的事实。


关于比较运算符的不错提示。让我想看看我的一些旧脚本,看看是否有可以使用的地方。特别是,这让我想起了Monty Hall模拟器的一部分。
— Iszi 2014年

1

Mathematica,37个字节

RandomChoice@DeleteCases[{1,2,3},a|b]

基本上与Ruby答案相同,但是由于Mathematica的函数名而使之更长。我使用的是小写变量,因为大写名称可能与内置函数发生冲突(在这种情况下,它们不会,但是您在Mathematica中根本不会这样做)。


1

R,42个字符

x=c(1,1,1);x[c(A,B)]=0;C=sample(1:3,1,p=x)

向量x是概率权重的向量,用于获得要采样的向量的元素。首先将其设置为1,然后将与A和B对应的元素设置为0,因此没有机会被拾取。



1

果酱-12

4,[AB0]-mr0=

根据问题,这假定变量A和B已经设置。

您可以在http://cjam.aditsu.net/上尝试

要使用随机数对其进行测试,请使用:

"A="3mr):A", B="3mr):B", C="
4,[AB0]-mr0=

要使用特定值对其进行测试,请使用(例如):

"A="1:A", B="1:B", C="
4,[AB0]-mr0=

说明:

4,创建数组[0 1 2 3]
[AB0]-从数组中删除数字A,B和0,
mr将剩余的数组
0=作为第一个元素

在将来的CJam版本中,该程序将短2个字节:)


1

C 67

int C(int a,int b){int c=0;while(c!=a&&c!=b)c=rand()%3+1;return c;}

1

JS,35岁

受布兰登·安扎尔迪(Brandon Anzaldi)的回答启发

A=1; // init
B=3; // init
do{C=1+new Date%3}while(C==A||C==B) // 35b

0

朱莉娅(32岁或56岁),取决于规则

julia> r()=rand(1:3);f(x...)=(i=r();i in x?f(x...):i)
julia> f(r(),r())

32,如果我不需要生成a和b。


问题是“假设已经为您选择了A和B”。
— nyuszika7h 2014年

0

JS,43岁

for(C=0;~[0,A,B].indexOf(C);)C=1+new Date%3

不会跑。你可能希望定义A和B第一。
— Spedwards

问题是:“假设您已经选择了A和B”。因此,您应该首先运行“ A = 1 + new Date%3; B = 1 + new Date%3”。
— xem 2014年


0

爪哇- 126 123 83 85(使用聪明c=6-a-b)

int c;if(a==b){int r=(int)(Math.random()*2);c=a==1?r+2:a==2?2*r+1:r+1;}else{c=6-a-b;}

完整版:

public void test(int a, int b) {
    int c;
    if (a == b) {
        // Random 0 or 1.
        int r = (int)Math.random()*2;
        c = // 1 -> 2 or 3
                a == 1 ? r + 2
                // 2 -> 1 or 3
                : a == 2 ? 2 * r + 1
                // 3 -> 1 or 2
                : r + 1;
    } else {
        // Whichever is not taken.
        //int[][] r = {{0, 3, 2}, {3, 0, 1}, {2, 1, 0}};
        //c = r[a - 1][b - 1];
        // Using @steveverrill's clever
        c = 6 - a - b;
    }
    System.out.println("a=" + a + " b=" + b + " c=" + c);
}

但是6-ab不是随机的,对吗?:)
— xem14'7

1
@xem-否-但仅6-a-b在没有其他选择时才使用,其他两个都被采用。它是由1,2,3采摘未选定数的一个聪明的方法6-1-2 = 3,6-1-3=2,6-2-3=1所以对于每个非等于A,B 6-a-b挑选第三个。聪明吗?您唯一可以使用random的时间是when a==b。
— OldCurmudgeon 2014年

哦,太好了。那好
— xem

0

R,24个字符

初始化

a = sample(1:3,1)
b = sample(1:3,1)

然后

n=1:3;n[!n%in%c(a,b)][1]

Or just n=1:3;n[!n%in%c(a,b)] but then you return both numbers.


0

R, 31 characters

sample(rep((1:3)[-c(A,B)],2),1)

If you do sample(x) in R, then it is interpreted as a random sample from 1:x. Repeating the vector (1:3)[-c(A,B)] twice is one way of stopping this from happening.


0

Javascript - 76

r=(y,z)=>Math.floor(Math.random()*(z-y+1)+y);a=b=r(1,3);while(c==a)c=r(1,3);

1
Math.floor can be replaced by ~~. You could also say r=(y,z)=>y+New Date(z-y). Also, A and B are not meant to be equal, and you don't have to count their initialization in your score.
— xem

@xem A and B are meant to be random. The rules say that they can be equal. Also, not sure about ES6 but y+New Date(z-y) is a SyntaxError
— Spedwards

@xem y+new Date(z-y) on the other hand reports new Date() string with y appended.
— Spedwards

sorry for the typo, I meant y+new Date%(z-y) ... or (z-y+1) depending on the params.
— xem

and my remark about A and B equal was about "a=b=r(1,3);". If you do that a is always equal to b, but that's not what the rules ask. They ask for A and B random. They can be equal but not necessarily
— xem


0

Java, 264 bytes

Random r = new Random();ArrayList<Integer> s = new ArrayList<>();ArrayList<Integer> q = new ArrayList<>();for(int i=0; i<n; i++) s.add(r.nextInt(k));q.add(s.get(r.nextInt(n)));q.add(s.get(r.nextInt(n)));int x;do{x = s.get(r.nextInt()); }while(!q.contains(x));

This code generates n different random numbers from 0 to k.


0

J (21 19: too long for my liking)

({~?@#)(>:i.3)-.A,B

Are there any J wizards around to help remove that variable assignment? It's only 2 chars shorter...

Or, if it doesn't have to be random, you could do with this:

{:(i.4)-.A,B

12 chars.



0

JavaScript - 41 (up to 46) 37 35 34 30

Updated:

Managed to get it down to 30 chars by modifying it, inspired by stevevarrill's answer in C.

C=A-B?6-A-B:1+(A+new Date%2)%3


Thank you nyuszika7h for getting me down to 34~:

C=A;while(C==A|C==B)C=1+new Date%3

Borrowing from xem's answer to at least fall on par with him:

C=A;while(C==A||C==B)C=1+new Date%3

Thanks for reminding me that 1+new Date%3 === (new Date%3)+1!

Previous Solution:

C=A;while(C==A||C==B)C=(new Date%3)+1

Ensure conditions of while() are satisfied, and loop through until they're not.


Other solution:

C=A!=B?6-A-B:A%2!=0?4-B:new Date%2!=1?3:1;

This assumes that C has already been declared OR that the JavaScript interpreter can handle undeclared variables.

However, if the JS interpreter can handle EOL without a semicolon, it could be bumped down to 41.

C=A!=B?6-A-B:A%2!=0?4-B:new Date%2!=1?3:1

If C hasn't been declared, and there is no error correction for it, that will bring the tally up to 46 characters.

var C=A!=B?6-A-B:A%2!=0?4-B:new Date%2!=1?3:1;

Test Program:

var iterations = 100;

for(var i = 0;i<iterations;i++) {
    var A = Math.floor(Math.random() * 3) + 1;
    var B = Math.floor(Math.random() * 3) + 1;
    C=A!=B?6-A-B:A%2!=0?4-B:new Date%2!=1?3:1
    if (C === A || C === B || C > 3 || C < 1) {
        console.log('FAILURE!');
        console.log(A + ',' + B + ',' + C)
        return;
    }
    console.log(A+','+B+','+C);
}

great! I made a 35b answer based on this, but using a do-while loop ;)
— xem

You should be able to use | instead of ||.
— nyuszika7h

Save 1 byte with a for loop: for(C=A;C==A|C==B;)C=1+new Date%3
— openorclose

I'd just like to thank everyone for their help :)
— Brandon Anzaldi

0

Befunge-98 (57 bytes)

This code assumes the numbers will be input on stdin. It will pick a random number if both of the first numbers are the same until it is different, otherwise it will choose the last available number.

6&::11p&:12pw>   ?1  >#<:11g-!_.@
     @.-g21-<>3;#[2#;^

-3

Python, 54 41 characters

Quite long answer in Python but I like list comprehension, so I decided to post this here

// [0] means it is the first element of

C=[i for i in[1,2,3]if not(i in(A,B))][0]

How is this random?
— user80551

It isn't, sorry...
— Caridorc

@user80551 does it need to be?
— John Dvorak

1
@JanDvorak Yes: "your job is to randomly pick a third number..." (I think this needs some more emphasis in the question, as many have gotten it wrong)
— daniero
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.