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

Pages: 1-4041-

On pseudorandom numbers and Tetris

Name: Anonymous 2009-11-15 11:24

So I'm programming a Tetris clone in Sepples and SDL. I need a method of generating a random number between 0 and 6 (seven different types of blocks) that gives a, more or less, even chance for each value to occur. I could use ctime and cstdlib to generate a pseudorandom number, then modulus that number by 7, but that wouldn't be an even chance. Some pieces would fall more often than others.

How does /prog/ get their pseudorandoms to be convincing?

Name: Anonymous 2009-11-15 11:33

I've never had a problem with rand() and srand() from C's standard library.

Name: Anonymous 2009-11-15 11:38

I just discovered that the Culver has deleted her groundbreaking paper on stellar rounding.

Name: Anonymous 2009-11-15 11:49

Tetris doesn't use a pseudorandom system but an elaborate bag system, which evolved over its history. A specific algorithm is required for current Tetris games, but current Tetris rules are utterly retarded because Henk Rogers is a nazi.

Name: Anonymous 2009-11-15 11:50

>>1
Have you read your C FAQ today? They have a whole section devoted to this.

Name: Anonymous 2009-11-15 11:51

(well, actually the bag system is pseudorandom, but rand() will make for a terrible! implementation of it)

Name: Anonymous 2009-11-15 12:01

Weird, I just wrote a Tetris clone earlier this week. Here's mine...

typedef int bag_t;

// Replace this with whatever you like; it should
// produce integers in the range [0, limit).
int
randomint(int limit)
{
    return rand() % limit;
}

bag_t
fillbag()
{
    int bag = 01234567;
    for (int n = 1; n < 7; n++) {
        int k = randomint(n + 1);
        int kv = (bag >> (3 * k)) & 7;
        int nv = (bag >> (3 * n)) & 7;
        bag &= ~((7 << (3 * k)) | (7 << (3 * n)));
        bag |= (kv << (3 * n)) | (nv << (3 * k));
    }
    return bag;
}

int
nextpiece(bag_t *pbag)
{
    if (*pbag == 0)
        *pbag = fillbag();
    int r = (*pbag) & 7;
    *pbag >>= 3;
    return r - 1;
}

Name: Anonymous 2009-11-15 12:03

>>7
VERY BAD
NOT USING MODULO
MODULO NOT RANDOM IN HIGH BITS
PLEASE DON'T USING MODULO IN FUTURE

Name: Anonymous 2009-11-15 12:03

>>1
You really think you'll be noticing a difference?
Even with the shitty random number generator in the standard library you would get an error that shouldn't be a problem for your use.

Name: Anonymous 2009-11-15 12:04

>>1
That's the problem: pseudorandom is intended to be taken as convincing.  Anything that gets in between the goal of selecting a number and the start makes the process suspect.  Also, any stable pseudorandom system will eventually break down and produce a sequence of the same number multiple times in a row.  Because that's random.  One thing that the C++ rand() function does warn is that lower numbers have a greater chance of being picked.

Having said that, you can always keep a count of the average of all random numbers spit out of the function.  Then, after you pass a certain number of iterations where you think the function isn't being "fair" anymore, you check whether the new rand() number is greater or less than the mean and add or subtract to it accordingly.  You add the modified number to the ongoing count and get the next number.

Name: Anonymous 2009-11-15 12:07

Oops, I just noticed that bag should really be of type bag_t.

Anyway, if you want 14 pieces per bag, just make bag_t at least 43 bits (int64_t is a portable and therefore sensible choice), and change the initializer to 012345671234567. A 7-piece bag seems to be entirely suitable, though.

Name: Anonymous 2009-11-15 12:08

>>8
ONLY ON FUCKING SHITTY OS
LEARN TO BETTER OS

Name: Anonymous 2009-11-15 12:09

Name: Anonymous 2009-11-15 12:22

>>12
MOTHERFUCKER IT SEEMS YOU GOT A PROBLEM
WELL NOW YOU HAVE TWO PROBLEMS

Name: Anonymous 2009-11-15 12:23

>>3
ITIHBTBSAH.
Does this mean she still browses /prog/ or did someone start trolling in the comments? I wouldn't put it past us to do the latter

Name: Anonymous 2009-11-15 13:03

>>7
// produce integers in the range [0, limit).
Wrong.  It will be 0 through limit-1.

Name: Anonymous 2009-11-15 13:09

>>16
2/10

Name: Anonymous 2009-11-15 13:42

"Admitting to and then exploring that connection was the WHOLE POINT of the book! It never ceases to amaze me how people think they can kill me with ammunition I GIVE THEM. It's rather like when I allow people to punch me in my toned torso."

"It's rather like when I allow people to punch me in my toned torso."

"It's rather like when I allow people to punch me in my toned torso."

"It's rather like when I allow people to punch me in my toned torso."

http://www.mdickie.com/blog.htm

Name: Anonymous 2009-11-15 13:45

>>18
Not programming releated.

Name: Anonymous 2009-11-15 13:47

>>18
http://www.amazon.com/review/RVO2I2BRIT2YO

( ≖‿≖)
(≖‿≖ )
( ≖‿≖ )

Name: Anonymous 2009-11-15 13:59

>>18
I draw attention to my insecurities because they've been conquered and are part of my past rather than my present. There aren't many people that can say that. I doubt you can.
I doubt you can.


Also, he really seems like he has issues.

Name: Anonymous 2009-11-15 15:30

>>21
I think he needs some serious psychological help.

Here's the comment that Dickie got epically trolled and butthurt by on Amazon:

Funny, funny, funny. For all the wrong reasons...
by The Pie Eater

So, a bit of background, I've been writing video games for 27 years, and Mat Dickie is a bit of a legend at the office - not for the games he creates, but for the frankly baseless belief he has in his own ability. All my colleagues were convinced he was a "character" - an avatar for someone he wanted to be; and so this thinly veiled autobiography appeared and I just had to read it. After a couple of chapters I thought, well, he's reasonably eloquent, maybe this character of a "maverick game developer" that he purports to be is just a bit of an act.

Several chapters in, and my word, this guy is a psychologist's dream - insecurities and over compensation for lack of social skills pour off the page - and in that regard it is a real page turner. I got more and more incredulous as I read through his thin tome; from his father's dalliances, to his love life. From his time in academia to his time "in the real world".

Some of the stories of game development in the book are truly cringe worthy and generally end with "unfortunately", as more and more ill-advised and badly conceived game software flops and flops again.

All in all, its not a bad book, and I'm sure in some ways it is inspirational - but not in the positive way that it alludes to. Its more of a series of excuses as to why a loner developer with delusions of grandeur and some serious psychosis failed terribly, but can't seem to accept that failure.

It certainly gets you into the mindset and thought process of young Mr Dickie, but its not a place any person would want to be.

Name: Anonymous 2009-11-15 16:37

>>22
Thanks for copying the content of the link in >>20 so that I could read it again. Much appreciated!

Name: Anonymous 2009-11-15 16:47

>>23
We extracted the post from your post so you can read after you read.

Name: Anonymous 2009-11-15 17:01

>>3
http://blog.leahculver.com/2008/11/couchdb-documents-python-objects.html
After the whole fuss about rounding numbers, I was considering changing the blog post to make myself look a bit better. I decided against this for several reasons:

1. My blog should reflect me and I do a lot of stupid things. For example, I regularly spill food on myself while eating. You would think that after 26 years of eating food, I'd be able to do that correctly. Obviously not.

2. It wasn't a big change. Really, like 3 lines of code. I had the right idea, even at 2 AM. I also knew I was probably doing something wrong. So yeah, not that bad.

3. It's good to be wrong sometimes. We all make mistakes. So now I'm declaring, yes, I wrote some stupid code and it's okay. It's okay for us all to write stupid code sometimes.*

* My blog was originally titled "Leah Culver's Stupid Blog" before I decided "Leah Culver" was more professional. I think I might change it back.

I guess in the end she couldn't even fool herself with those lame-ass excuses, ololol.

Name: Anonymous 2009-11-15 17:10

So which pairing you prefer, LeahxMat or LeahxCollision? I think I'll write a fanfic.

Name: Anonymous 2009-11-15 17:20

>>26
XarnxLeahxSussman threeway

Name: Anonymous 2009-11-15 17:32

tl;dr generate a random permutation of (0 .. 6) over and over

Name: Anonymous 2009-11-15 17:36

>>27
We conjure the spirits of our sexuality with our spells?

Name: Anonymous 2009-11-15 17:44

>>26
Mat/Collision

Name: Anonymous 2009-11-15 17:57

>>26
SussmanxDickie

Name: Anonymous 2009-11-15 17:59

computerfucks are obsessed wth "On ___"

Name: Anonymous 2009-11-15 18:06

>>32
Personally, I've seen it more often on philosophical musings, but there you go.

Name: Anonymous 2009-11-15 18:08

>>26
Dickie vs Vulva would be more entertaining by far.

Name: Anonymous 2009-11-15 18:13

>>32
Philosopharts do it too

Name: Anonymous 2009-11-15 18:20

And so goes the spread of mediocrity. Hope one day it will get killed by fire.

Name: Anonymous 2009-11-15 18:32

>>35
how dare you compare a noble profession such as programming to the pathetic joke that is philosophy

Name: Anonymous 2009-11-15 19:40

>>32
On Anii

Name: Anonymous 2009-11-15 23:46

>>38

On Anii and the Haxing of The Aforementioned

Name: Anonymous 2009-11-16 2:25

andrey@andrey-linux:~$ perl -e '$a[int 7*rand]++ foreach 1..10_000_000; print join " ",@a[0..6],"\n"'
1429676 1429456 1426992 1429611 1428681 1425877 1429707
andrey@andrey-linux:~$ perl -e '$a[int(0x10000*rand)%7]++ foreach 1..10_000_000; print join " ",@a[0..6],"\n"'
1428773 1428597 1428502 1427044 1429391 1429428 1428265
andrey@andrey-linux:~$


Perl uses srand and rand. You can write similar program in C and see for yourself. Why did you even think that it wouldn't be an even chance?

Name: !Xarn.T4glk 2009-11-16 3:08

>>27
I'm in!

Name: Anonymous 2009-11-16 3:20

>>40
Yawn. Close enough. This isn't fuckin' crypto.

Name: Anonymous 2009-11-16 8:35

>>40
But it's not.  There are 306783379 ways to pick (each of) 0 or 1, but only 306783378 ways to pick 2 through 6.

Name: ​​​​​​​​​​ 2010-10-24 3:09

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