Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

prog challenge

Name: Anonymous 2010-09-13 8:38

Create a sequence of random integers with the following given: max, average, length

Example, with m = 7, a = 4, l = 5, you get [ 5, 2, 2, 4, 7 ].

Name: Anonymous 2010-09-13 8:40

It's not "random" if the average is anything but (* (+ min max) 0.5)

Name: Anonymous 2010-09-13 8:47

>>2
For length->oo in a discrete countable domain (integers for example), that is true, but irrelevant.

If you want another definition for random:
If I run the code enough times, I'll get all such possible sequences, and not any more.

Name: Anonymous 2010-09-13 9:07

span = 2 * (max - average)
min = max - span
[min + rand(span) for _ in length]


sewdo-code

Name: Anonymous 2010-09-13 9:20

>>4
Try it, won't work. Assuming rand(span) returns anything from 0 to span, it's possible that all rand's return 0.
Therefore your sequence is made of min's only and its average is min and not average, but min is equal max - 2average, so they're not necessarily equal

Name: Anonymous 2010-09-13 9:34

>>4,5
while ((sum (result) / length) != average) result = [ min + rand(span) for _ in length ]

Name: Anonymous 2010-09-13 10:12

I don't know. My NP-hard senses are tingling.

Name: Anonymous 2010-09-13 10:17

replicateM length $ randomRIO (min, max)

Name: Anonymous 2010-09-13 10:25

>>7
Protip: the problem is NP-hard because OP is forcing you to use the same random generator you use for the numbers with its properties in the rest of your program. It's a form of a vicious cycle. If you assume two rands, pseudorand() and truerand(), then it's easily solvable.

Name: Anonymous 2010-09-13 10:49

>>8
Boy, did I not read the specification there.

Oh, well.

getAvgRand len avg max = fix (\f (x:xs) -> x >>= maybe (f xs) return) $ map (\x -> sequence x >>= \y -> if (fromIntegral $ sum y) / fromIntegral len == avg then return (Just y) else return Nothing) (repeat $ take len $ repeat $ randomRIO (0, max))

Name: Anonymous 2010-09-13 10:51

plonk plonk

Name: Anonymous 2010-09-13 10:52

plonk plonk

Name: Anonymous 2010-09-13 12:38

>>1
Wow, it's a good challenge for a change, thank you OP.

Also, shows how many morons there are on [spoiler]\gorp[/spoiler].

Interesting fact: it becomes almost obvious (as in, "I've done this before") as soon as you replace "average" (which is floating-point and therefore sucks) with "sum". Then all that you need to do is to enumerate all such sequences (i.e. get the total number and construct the function index -> sequence, see generation of n-th transposition), then select a random number in that range.

Obviously, as a Systems Architect I'm not going to write the code. If a dimwit like >>10,8 can understand my superior design then he is welcome to implement it. Though I highly doubt he would be up to the task, he seems to have been exposed to haskal for long enough to be irreversibly brain damaged.

Name: >>13 2010-09-13 12:50

[spoiler]\hsalskcab\[/spoiler]

Name: >>14 2010-09-13 12:52

lulz, shitchan. The backslash got backslasher'd, which did not prevent it from backslashing the bracket.

Name: Anonymous 2010-09-13 13:00

getAvgRand m a l | a > m = error "average cannot be greater than max"
                 | otherwise = replicate l a

Name: Anonymous 2010-09-13 13:02

>>16
Ding ding, a winner!

Name: Anonymous 2010-09-13 13:17

from random import randrange as rand
m = int(raw_input("max: "))
a = int(raw_input("average: "))
l = int(raw_input("length: "))

seq = [a] * l
while max(seq) < m:
    seq[rand(l)] -= 1
    seq[rand(l)] += 1
print seq

Name: Anonymous 2010-09-13 13:39

>>18
Good work, although you can obtain sequences with non-positive numbers this way.

Name: Anonymous 2010-09-13 13:54

I nominate >>18 (inb4 lisp implementation of the same thing by a lisper without creativity if you will excuse the tautology) for winner.

Name: Anonymous 2010-09-13 14:11

haxus$ >>18
max: 2
average: 1
length: 20
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1]


EXPERT RANDOMNESS

Name: Anonymous 2010-09-13 14:18

>>21
That 2 and the 0 in there are pretty random.  I didn't expect that.

Name: Anonymous 2010-09-13 14:52

>>21
You know it only touched that 0 and then the 2 and then quit, right?

Name: >>18 2010-09-13 15:29

>>19
Replacing the while loop with
while max(seq) < m:
    move = rand(l)
    if move_from > 0:
        seq[move] -= 1
        seq[rand(l)] += 1

should fix that.

Name: Anonymous 2010-09-13 15:53

Sigh.
ihbt :(

Name: >>13 2010-09-14 10:49

OK, bitches, I've designed my share of enterprise turkey three-sixes scaly rhombus solutions today.

http://pastebin.com/UQgGuJpm

You all are morons btw.

Name: Anonymous 2010-09-14 12:29

Using a pastebin on a programming BBS

Name: >>24 2010-09-14 14:56

Wait, move and move_from are supposed to be the same thing. How the fuck did I mess up five lines of code?

Name: Anonymous 2010-09-14 15:11

>>1
Challenge is impossible.  No such thing as a "sequence of random integers" from a program.  Pseudorandom, maybe.

Name: Anonymous 2010-09-14 16:32

>>29
Program executes on a computing machine that has a lot of sources of true randomness.

Name: Anonymous 2010-09-14 17:24

{ ((1 + $^a - 2 * $^b) .. $^a ).pick($^c, :replace) }(7,4,5)

Or are we supposed to enforce the "average" value is actually the average? Rakudo still shits itself every time I try anything interesting with post-selection.

Name: Anonymous 2010-09-16 4:58

l=1 possibilities
max = average

l=2 possibilities
average = (max + n)/2
2*average = max + n
n = 2*average - max
n ≤ max

the following relationship must exist between the max and average for any valid situation
average ≤ max

l=3 possibilites
average = (max + n + m)/3
3*average-max=n+m
...

so

length*average-max=∑(other values which are ≥ max)

Name: Anonymous 2010-09-16 4:59

Name: Anonymous 2010-09-16 5:06

>>30
So in order for GCC to work you have to have a working RNG, e. g. Quantis PCI QRNG

Name: Anonymous 2010-09-16 5:12

>>34
get out, Quantis devs

Name: Anonymous 2010-09-16 7:30

random integers
Until this definition is clarified further I will not participate. A randomly picked integer will almost surely contain more information than is possible to represent in this universe, even when it has a fixed maximum constraint.

Name: Anonymous 2010-09-16 8:36

>>36
Nonnegative of course.

By the way, let me assure you that you didn't come out witty, on the contrary, you are a typical victim of the multiple-choice-based education system, utterly unable to think otherwise than in the helpfully provided bounds.

Name: Anonymous 2010-09-16 9:52


def stupid_prague_challenge(m, a, l):
    from random import randint
    while True:
        r = [randint(1, m) for k in range(l)]
        if (sum(r) / l) == a:
            return r

Name: Anonymous 2010-09-16 11:26

it's supposed to give you also negative integers, especially if you choose max ⋙ avg. amirite?

Name: Anonymous 2010-09-16 12:10

>>37
Eat shit.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List