Random Numbers?
Okay..... I know this isn''t about a game, but it could be useful to know once I start programming games, and I''m definently interested in game programming.
Anyways, I''m making this thing that brings up flashcards of music notes. Everytime you guess a note(right or wrong), it generates a number from 0 to 12 for a new note. My problem was it kept playing the same note twice in a row, so I added this code in to make sure that didn''t happen anymore:
void Right()
{
mScore++;
SetDlgItemText(IDC_BTN_GO, "RIGHT!");
while(mNote == mOldNote)
{
srand( (unsigned)time( NULL ) );
mNote = rand() % 13;
}
Invalidate();
mOldNote = mNote;
}
Now, I am faced with a new problem. It keeps generating notes in patterns. For example: it will generate a 0, then a 7, then a 0, then a 7. The pattern will change slowly, but it''s still pretty bad. Why is this happening? Is there any way I can prevent it?
-Forcas
-Forcaswriteln("Does this actually work?");
quote: Original post by Forcas
Okay..... I know this isn''t about a game, but it could be useful to know once I start programming games, and I''m definently interested in game programming.
Anyways, I''m making this thing that brings up flashcards of music notes. Everytime you guess a note(right or wrong), it generates a number from 0 to 12 for a new note. My problem was it kept playing the same note twice in a row, so I added this code in to make sure that didn''t happen anymore:
void Right()
{
mScore++;
SetDlgItemText(IDC_BTN_GO, "RIGHT!");
while(mNote == mOldNote)
{
srand( (unsigned)time( NULL ) );
mNote = rand() % 13;
}
Invalidate();
mOldNote = mNote;
}
Now, I am faced with a new problem. It keeps generating notes in patterns. For example: it will generate a 0, then a 7, then a 0, then a 7. The pattern will change slowly, but it''s still pretty bad. Why is this happening? Is there any way I can prevent it?
-Forcas
Are you using the srand() function before you use rand()?
If not, doing that first should help. Also, don''t re-seed the random function again. That''s why your getting the patterns.
HTH,
Dave "Dak Lozar" Loeser
Dave Dak Lozar Loeser
"Software Engineering is a race between the programmers, trying to make bigger and better fool-proof software, and the universe trying to make bigger fools. So far the Universe in winning."--anonymous
"Software Engineering is a race between the programmers, trying to make bigger and better fool-proof software, and the universe trying to make bigger fools. So far the Universe in winning."--anonymous
quote: Original post by Forcas
mScore++; SetDlgItemText(IDC_BTN_GO, "RIGHT!"); while(mNote == mOldNote) { srand( (unsigned)time( NULL ) ); mNote = rand() % 13; } Invalidate(); mOldNote = mNote;}
-Forcas
Call srand only once at the start of your program, not every time you want a number.
---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement