I am trying to program a game called Bubble Poke, and I have run into a small problem. The rand() function in toolchain is not random. It's the same every time. Every time I run the program, I get the same exact matrix values, even though I used rand() in a nested for loop. Is there a special command in toolchain that can assist me in making rand() actually random instead of predictable, or will I have to stick with a predictable matrix every time?

Here is the source code if you're interested:


Code:
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tice.h>

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <keypadc.h>
#include <graphx.h>
#include "gfx/logo_gfx.h"

void main(void){

    int dots[10][10];
    uint8_t i=1;
    uint8_t j=1;
    uint8_t color=1;
    uint8_t randomNumber=1;

    for(i=0;i<10;i++){
        for(j=0;j<10;j++){
            randomNumber = (rand()%2)+1;
            if(randomNumber == 2) color++;
            if(color == 7) color = 1;
            if(color == 1) dots[i][j] = 224;
            if(color == 2) dots[i][j] = 227;
            if(color == 3) dots[i][j] = 231;
            if(color == 4) dots[i][j] = 38;
            if(color == 5) dots[i][j] = 24;
            if(color == 6) dots[i][j] = 153;
        }
    }

    gfx_Begin();
    gfx_SetPalette(logo_gfx_pal, sizeof_logo_gfx_pal, 0);
    gfx_FillScreen(1);
    for(i=0;i<10;i++){
        for(j=0;j<10;j++){
            gfx_SetColor(dots[i][j]);
            gfx_FillCircle_NoClip(20*i+20,20*j+20,9);
        }
    }

    delay(5000);


    gfx_End();
    os_ClrHome();

}
Computers don't (usually) produce truly random numbers. Instead, they use a pseudorandom number generator (PRNG). Even rand() in BASIC is only pseudorandom. Your problem is that you need to seed the PRNG using srand(). Typically, computer programs will use the current time. So at the start of your program put something like:
Code:
srand(rtc_Seconds + rtc_Minutes * 60 + rtc_Hours * 60 * 60 + rtc_Days * 60 * 60 * 24);
Typically, you'd use the C function time() but I didn't see it in my cursory glance through the SDK.
srand(rtc_Time());

As per the toolchain examples Very Happy
Adriweb wrote:
srand(rtc_Time());

As per the toolchain examples Very Happy

^
I am getting an error when I try to "make" the file.

ERROR (154) Left expression to "%" must be integral type

This is the code that I'm using:

Code:
randomNumber = (srand(rtc_Time())%2)+1;


HELP!

EDIT:
Apparently srand is a function that sets the seed, not gives the random number lol I'm stupid.


Code:
srand(rtc_Time());
    for(i=0;i<10;i++){
        for(j=0;j<10;j++){

            randomNumber = (rand()%2)+1;
            if(randomNumber == 2) color++;
            if(color == 7) color = 1;
            if(color == 1) dots[i][j] = 224;
            if(color == 2) dots[i][j] = 227;
            if(color == 3) dots[i][j] = 231;
            if(color == 4) dots[i][j] = 38;
            if(color == 5) dots[i][j] = 24;
            if(color == 6) dots[i][j] = 153;
        }
    }


That's more like it. Thanks guys!
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement