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:
Anonymous2009-11-15 11:33
I've never had a problem with rand() and srand() from C's standard library.
Name:
Anonymous2009-11-15 11:38
I just discovered that the Culver has deleted her groundbreaking paper on stellar rounding.
Name:
Anonymous2009-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:
Anonymous2009-11-15 11:50
>>1
Have you read your C FAQ today? They have a whole section devoted to this.
(well, actually the bag system is pseudorandom, but rand() will make for a terrible! implementation of it)
Name:
Anonymous2009-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:
Anonymous2009-11-15 12:03
>>7
VERY BAD
NOT USING MODULO
MODULO NOT RANDOM IN HIGH BITS
PLEASE DON'T USING MODULO IN FUTURE
Name:
Anonymous2009-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:
Anonymous2009-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.
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.
"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."
>>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:
Anonymous2009-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:
Anonymous2009-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:
Anonymous2009-11-15 16:47
>>23
We extracted the post from your post so you can read after you read.
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:
Anonymous2009-11-15 17:10
So which pairing you prefer, LeahxMat or LeahxCollision? I think I'll write a fanfic.