| Bytes | Lang | Time | Link |
|---|---|---|---|
| 019 | MATL | 160707T163758Z | Luis Men |
| 133 | C gcc | 230604T140525Z | Peter |
| 205 | Scala 3 | 230604T021612Z | 138 Aspe |
| 056 | Mathematica | 160707T155624Z | miles |
| 224 | Java 8 | 160708T045406Z | R. Kap |
| 030 | J | 160707T232009Z | miles |
| 103 | Python 3 with NumPy | 160707T210556Z | TheBikin |
| 116 | JavaScript ES6 | 160707T204315Z | Neil |
| 025 | CJam | 160707T155631Z | Martin E |
| 048 | Julia | 160707T172158Z | Dennis |
| 134 | C# | 160707T170648Z | aloisdg |
| 015 | Jelly | 160707T164957Z | Dennis |
| 181 | JavaScript | 160707T161519Z | charredg |
| 024 | Pyth | 160707T155206Z | Leaky Nu |
MATL, 20 19 bytes
251tliq3$Yrihi:ZQw\
Input order is t, s, n.
Explanation
251t % Push 251 twice
l % Push 1
iq % Take input t. Subtract 1
3$Yr % Generate t-1 random integers in [1 2 ... 251]
ih % Take input s. Concatenate with the random integers
i: % Take input n. Generate range [1 2 ... n]
ZQ % Evaluate polynomial at those values
w % Swap to move copy of 251 to the top of the stack
\ % Modulo. Implicitly display
C (gcc), 133 bytes
-10 bytes thanks to @ceilingcat!
-1 byte thanks to @ceilingcat!
v[999];i,j,k,l;f(n,t,s){for(i=t;--i;)v[i]=rand()%251;for(*v=s;n/++i;printf("%d ",s%251))for(s=j=0;j<t;s+=k)for(k=v[l=j++];l--;)k*=i;}
Scala 3, 205 bytes
\$ \text{205 bytes, It can be golfed much more.} \$
Golfed version. Try it online!
(n,t,s)=>{val W=new Array[Int](t-1);for(i<-0 until t-1){W(i)=Random.nextInt(251)};val O=new Array[Long](n);for(i<-1 to n){var T=0L;for(h<-1 until t) {T=T+(W(h-1)*math.pow(i,h)).toLong};O(i-1)=(T+s)%251};O}
Ungolfed version. Try it online!
import scala.util.Random
object A {
type G = (Int, Int, Int) => Array[Long]
def main(args: Array[String]): Unit = {
val J: G = (n, t, s) => {
val W = new Array[Int](t - 1)
for (i <- 0 until t - 1) {
W(i) = Random.nextInt(251)
}
val O = new Array[Long](n)
for (i <- 1 to n) {
var T = 0L
for (h <- 1 until t) {
T = T+ (W(h - 1) * math.pow(i, h)).toLong
}
O(i - 1) = (T + s) % 251
}
O
}
val scanner = new java.util.Scanner(System.in)
while (scanner.hasNextLine) {
val n = scanner.nextInt()
val t = scanner.nextInt()
val s = scanner.nextInt()
println(J(n, t, s).mkString("[", ", ", "]"))
}
}
}
Mathematica, 59 56 bytes
Mod[Power~Array~{#2,#-1}.RandomInteger[250,#-1]+#3,251]&
Takes three arguments in the order t, n, and s. Constructs a 2d-array with n rows and t-1 columns. Each row vector j, numbered from 1 thru n, contains the powers of j thru jt-1. Then a vector of random integer coefficients in the range 0 thru 250 is created with t-1 values. That is matrix-multiplied with the 2d-array, and then s is added element-wise and taken module 251 to get the value of the polynomial at each of the n points.
Java 8, 224 bytes:
(n,t,s)->{int[]W=new int[t-1];for(int i=0;i<t-1;i++){W[i]=new java.util.Random().nextInt(251);};long[]O=new long[n];for(int i=1;i<=n;i++){long T=0;for(int h=1;h<t;h++){T+=W[h-1]*Math.pow(i,h);}O[i-1]=((T+s)%251);}return O;};
A Java 8 lambda expression. Outputs a comma separated integer array, and works perfectly until values in the output array reach beyond the range of the Java's long, or 64-bit signed integer, data type, upon which -200 is output into the array.
J, 32 30 bytes
251|(1+i.@{.)p.~{:0}251?@#~1&{
Takes a list with the values n, t, and s.
Saved 2 bytes by using the replace at index 0 idea from @Dennis's solution.
Explanation
251|(1+i.@{.)p.~{:0}251?@#~1&{ Input: [n t s]
1&{ Select at index 1 (t)
251 #~ Create that many copies of 251
?@ Generate that many random integers in [0, 251)
{: Get the tail of the input (s)
0} Replace the value at index 0 of the random integer list
with s to make a coefficient list of the polynomial
{. Get the head of the input (n)
i.@ Make the range [0, n-1]
1+ Add 1 to each to get [1, n]
p.~ Evaluate the polynomial at each value [1, n]
251| Take each value mod 251 and return
Python 3 with NumPy, 103 bytes
from numpy import*
lambda n,t,s:[poly1d(append(random.randint(0,251,t-1),s))(i+1)%251for i in range(n)]
I can honestly say that I never expected to use NumPy for code golf...
An anonymous function that takes input via argument and returns a list.
How it works
from numpy import* Import everything in the NumPy library
lambda n,t,s... Function with input number of players n, threshold value t and
secret s
random.randint(0,251,t-1) Generate a NumPy array R of t-1 random integers in [0,250]
append(...,s) Append s to R
poly1d(...) Generate a polynomial p of order t-1 with coefficients R and
constant term s
...for i in range(n) For all integers i in [0,n-1]...
...(i+1) ...evaluate p(i+1), so for all integers in [1,n]...
...%251 ...and take modulo 251
...:[...] return as list
JavaScript (ES6), 116 bytes
(n,t,s)=>[...Array(n)].map((_,i)=>++i&&t.reduce((r,a)=>r*i+a)%251,t=[...Array(t)].map(_=>--t?Math.random()*251|0:s))
I'd like to think this is one of the rare cases where reduce beats map.
C#, 138 134 bytes
(n,t,s)=>new int[n+1].Select((_,x)=>(s+new int[t-1].Select(k=>new Random(e).Next(251)).Select((c,i)=>c*Math.Pow(x+1,i+1)).Sum())%251);
C# lambda where inputs are int and output is an IEnumerable<double>. You can try my code on .NetFiddle.
I am not 100% sure about the validity of my algorithm, please comment if I misunderstood something.
4 bytes saved with @raggy's trick.
Jelly, 15 bytes
251©xX€⁵0¦ḅЀ%®
Expects t, n, and s as command-line arguments. Try it online!
How it works
251©xX€⁵0¦ḅЀ%® Main link. Left argument: t. Right argument: n Third argument: s
251© Yield 251 and copy it to the register.
x Repeat [251] t times.
X€ Random choice each; pseudo-randomly choose t integers from
[1, ..., 251]. Since 251 = 0 (mod 251), this is equivalent to
choosing them from [0, ..., 250].
⁵0¦ Replace the last generated integer (index 0) with s (⁵).
ḅЀ Interpret the resulting array as a base-k number, for each k in
[1, ..., n], and convert to integer.
® Yield 251 from the register.
% Take the generated integers modulo 251.
JavaScript, 181 bytes
(n,t,s)=>{r=Array(t-1).fill(0).map($=>{return Math.random()*251});f=(x=>{p = 0;r.map((k,c)=>p+=k*Math.pow(x, c));return s+p});_=Array(t-1).fill(0);_.map((l,i)=>_[i]=f(i));return _;}
Ungolfed:
(n, t, s) => {
r = Array(t - 1).fill(0).map($ =>{return Math.random() * 251});
f = (x => {
p = 0;
r.map((k, c) => p += k * Math.pow(x, c));
return s + p
});
_ = Array(t - 1).fill(0);
_.map((l, i) => _[i] = f(i));
return _;
}
I don't know how to properly check it, but I do know that it was a pain to get JS to map across a new array since apparently .map skips undefined values. If anyone sees any ways to improve, or flaws, don't hesitate to let me know.