Omnimaga

Calculator Community => TI Calculators => Calculator C => Topic started by: kevinkore3 on June 17, 2014, 05:24:21 pm

Title: good random numbers
Post by: kevinkore3 on June 17, 2014, 05:24:21 pm
I'm trying to make a drilling game. When the player first starts, the program generates some dirt blocks with minerals 50 rows deep and 40 columns wide, or 2000 blocks. However, using time as rand gives me really crappy number generation (all the blocks in a column are the exact same if I use time as rand). I've tried using a couple other things, like using SDL_GetTicks() (which produces the exact same thing every time by itself, so I also need rand) and xoring the int a couple of times, but I still get really poorly generated blocks. Also, I still want it to be simple enough that it doesn't lagg much. Any ideas?




My current block generator:

Code: [Select]

void newrow(uint16_t row)
{
srand(time(NULL));
chance[0]=1000/(abs(pow(row-10,1.1))+50);//1000/(abs(pow(row-row_where_it_is_most_common,range_of_how_common_it_is))+100/percent_of_blocks_in_most_common_row)
//coal is found with a frequency of 100/50=2% of blocks in the row in which it is most commonly found
//coal is most commonly found in row 10 (actually 11 in the array, but 1 more or less doesn't make a huge difference)
//coal is commonly found in a large range of blocks, note the low exponent
//note that 0 in this array corresponds to 2 in blocktype
chance[1]=1000/(abs(pow(row-30,1.1))+60);//iron is found in 1.667% or 16/1000 blocks in the generator at max, and is also very common
chance[2]=1000/(abs(pow(row-50,1.1))+80);
chance[3]=1000/(abs(pow(row-80,1.1))+20);
chance[4]=1000/(abs(pow(row-110,1.3))+100);
chance[5]=1000/(abs(pow(row-150,1.5))+150);
for(int i=0;i<40;i++)
{
tempint=((SDL_GetTicks()+rand()+i)^(row*i))%1000;
sleep(1);
for(int z=0;z<6;z++)
{
if(z==5)
{
if(rand()%8)
{
map[row][i]=dirt;
break;
}
map[row][i]=nothing;
break;
}
if(chance[z]>tempint)
{
map[row][i]=(static_cast<blocktype>(z+2));
break;
}
tempint-=chance[z];
}
}
}
Title: Re: good random numbers
Post by: Sorunome on June 17, 2014, 05:25:48 pm
Shouldn't your language have pseudo-random built-in?
Title: Re: good random numbers
Post by: kevinkore3 on June 17, 2014, 05:26:31 pm
Shouldn't your language have pseudo-random built-in?
Yes, I used srand(time(NULL)) but that gives me total crap for blocks :(
I was wondering if there was a better thing for generation
Title: Re: good random numbers
Post by: Sorunome on June 17, 2014, 05:26:52 pm
you only set the seed once all at the beginning of your program.
Title: Re: good random numbers
Post by: AngelFish on June 17, 2014, 05:28:10 pm
Is there a reason you're calling srand(time(NULL)); every time you initialize a new row? It should be called once at the beginning of the program and then subsequent calls to srand will give you proper random numbers. You should not be reseeding rand multiple times without a very good reason. The way you have it set up, srand is set to the same value every time you call the rows, which is obviously why you're not getting good results.
Title: Re: good random numbers
Post by: kevinkore3 on June 17, 2014, 05:32:57 pm
you only set the seed once all at the beginning of your program.
Woops
Epic fail
Looks like I still have much to learn
I spent a long time looking at the code, and just realized I added the line in the wrong place
Title: Re: good random numbers
Post by: Sorunome on June 17, 2014, 05:33:59 pm
you only set the seed once all at the beginning of your program.
Woops
Epic fail
Looks like I still have much to learn
Hehe, no worries, we all fail, and the important thing is that you learn from them.
Also, glad I could help out :)
Title: Re: good random numbers
Post by: Streetwalrus on June 17, 2014, 06:00:15 pm
Havege. :trollface:
Nah jk. There are good prng algorithms out there. Google around Wikipedia. :P
Title: Re: good random numbers
Post by: bb010g on June 17, 2014, 06:21:01 pm
(http://imgs.xkcd.com/comics/random_number.png)
Title: Re: good random numbers
Post by: Sorunome on June 17, 2014, 06:43:05 pm
xkcd always does it ;)
Title: Re: good random numbers
Post by: Juju on June 17, 2014, 06:46:26 pm
I was about to say that too.

Otherwise, you can try integrating a library such as this one: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
Title: Re: good random numbers
Post by: pimathbrainiac on June 17, 2014, 08:36:04 pm
Since this looks a lot like Drillminer (basically motherload with MC block skins), you could ask epic7 for the source so you can get some help with this issue, and engine stuffs.
Title: Re: good random numbers
Post by: kevinkore3 on June 19, 2014, 11:08:47 pm

(http://imgs.xkcd.com/comics/random_number.png)
That'd be a really troll map generated by that :D
I was about to say that too.


Otherwise, you can try integrating a library such as this one: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
time is good enough for now
Since this looks a lot like Drillminer (basically motherload with MC block skins), you could ask epic7 for the source so you can get some help with this issue, and engine stuffs.
I'm pretty much trying to make a (bad) clone of motherload :P


Anyway, the blocks are much better now
Thanks for your suggestions

Title: Re: good random numbers
Post by: DJ Omnimaga on June 19, 2014, 11:30:22 pm
Looks nice so far by the way :D
Title: Re: good random numbers
Post by: Sorunome on June 20, 2014, 12:27:18 am
Looks nice so far by the way :D
Yup, looking nice!
Title: Re: good random numbers
Post by: kevinkore3 on June 21, 2014, 02:13:03 pm
Sorry for all the posts, but I have another problem :L
Code: [Select]
player.position_x=player.position_x_temp-player.drillcount/player.drillmax;
doesn't do anything because drillmax>drillcount, so drillcount/drillmax=0, but
Code: [Select]
player.position_x=player.position_x_temp-20*player.drillcount/player.drillmax;
moves the player 40 pixels to the left (which is very strange because it should be at most 20)

I tested
Code: [Select]
player.position_x=player.position_x_temp-1.1*player.drillcount/player.drillmax;and it also moves the player 40 pixels to the left ???
Code: [Select]
#include <os.h>
#include <SDL/SDL.h>
#include <ctime>
#include <cmath>
#include "blocks.cpp"
#include "player.cpp"
#define score_color 0x8000
#define spacing 12
uint32_t *money=0;
uint16_t tempint;
uint16_t chance[18];//for ore generation
uint8_t getlength(int score)
{
   uint8_t scorelength;
   while(1)
   {
      if(score>9)
      {
         if(score>99)
         {
            if(score>999)
            {
               if(score>9999)
               {
                  if(score>99999)
                  {
                     scorelength=6; //gl reaching 7 digits
                     break;
                  }
                  scorelength=5;
                  break;
               }
               scorelength=4;
               break;
            }
            scorelength=3;
            break;
         }
         scorelength=2;
         break;
      }
      scorelength=1;
      break;
   }
   return scorelength;
}
void shownumber(SDL_Surface *screen,uint32_t score,uint8_t scorelength)
{
   uint8_t tempscore;
   score*=10;
   for(int i=0;i<scorelength;i++)
   {
      score/=10;
      tempscore=score%10;
      switch(tempscore)
      {
         case 0:
            for(int z=0;z<15;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i,z,score_color);
               nSDL_SetPixel(screen,315-spacing*i,z,score_color);
            }
            for(int z=1;z<11;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,0,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,14,score_color);
            }
            break;
         case 1:
            for(int z=0;z<15;z++)
            {
               nSDL_SetPixel(screen,310-spacing*i,z,score_color);
            }
            break;
         case 2:
            for(int z=0;z<11;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,0,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,7,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,14,score_color);
            }
            for(int z=0;z<8;z++)
            {
               nSDL_SetPixel(screen,315-spacing*i,z,score_color);
               nSDL_SetPixel(screen,305-spacing*i,7+z,score_color);
            }
            break;
         case 3:
            for(int z=0;z<15;z++)
            {
               nSDL_SetPixel(screen,315-spacing*i,z,score_color);
            }
            for(int z=0;z<10;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,0,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,7,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,14,score_color);
            }
            break;
         case 4:
            for(int z=0;z<15;z++)
            {
               nSDL_SetPixel(screen,315-spacing*i,z,score_color);
            }
            for(int z=0;z<10;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,7,score_color);
            }
            for(int z=0;z<7;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i,z,score_color);
            }
            break;
         case 5:
            for(int z=0;z<7;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i,z,score_color);
               nSDL_SetPixel(screen,315-spacing*i,7+z,score_color);
            }
            for(int z=0;z<10;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,0,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,7,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,14,score_color);
            }
            break;
         case 6:
            for(int z=0;z<10;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,0,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,7,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,14,score_color);
            }
            for(int z=0;z<15;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i,z,score_color);
            }
            for(int z=0;z<8;z++)
            {
               nSDL_SetPixel(screen,315-spacing*i,7+z,score_color);
            }
            break;
         case 7:
            for(int z=0;z<15;z++)
            {
               nSDL_SetPixel(screen,315-spacing*i,z,score_color);
            }
            for(int z=0;z<10;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,0,score_color);
            }
            break;
         case 8:
            for(int z=0;z<10;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,0,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,7,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,14,score_color);
            }
            for(int z=0;z<15;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i,z,score_color);
               nSDL_SetPixel(screen,315-spacing*i,z,score_color);
            }
            break;
         case 9:
            for(int z=0;z<10;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i+z,0,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,7,score_color);
               nSDL_SetPixel(screen,305-spacing*i+z,14,score_color);
            }
            for(int z=0;z<8;z++)
            {
               nSDL_SetPixel(screen,305-spacing*i,z,score_color);
            }
            for(int z=0;z<15;z++)
            {
               nSDL_SetPixel(screen,315-spacing*i,z,score_color);
            }
            break;
      };
   }
}
enum blocktype
{
   nothing=0,
   dirt=1,
   coal=2,
   iron=3,
   tin=4,
   copper=5,
   silver=6,
   gold=7,
   platinum=8,
   topaz=9,
   sapphire=10,
   emerald=11,
   ruby=12,
   opal=13,
   jade=14,
   diamond=15,
   fossil=16,
   artifact=17,
   sigsegv=18,
   lava=19,//visible but does lots of damage
   rock=20,//you have to blow these up with explosives
   gas=21//invisible and blows up if you run into it
};
blocktype map[600][40];//motherload is something like 12 feet a block-1 block is 20 pixels
//less than 100kB anyway, no point using push_back so often
enum currentdirection
{
   down=0,
   right=1,
   left=2,
   up=3
};
class theplayer
{
public:
   bool is_flying;
   SDL_Rect rect_player_centered;
   SDL_Rect rect_player_animation_source;
   uint16_t drillmax;
   uint16_t drillcount;
   uint8_t animationcount;
   bool is_active;
   currentdirection direction;
   int8_t acceleration;
   int16_t position_x;//in pixels
   int16_t position_x_temp;
   int16_t depth;//in pixels
   int16_t depth_temp;
   uint8_t hull;
   uint8_t hullhealth;
   uint8_t drillspeed;
   uint8_t drill;
   uint16_t fueltank;
   uint16_t fuel;//every loop-1L=100 loops=2 seconds at 50FPS
   uint8_t engine;
   uint8_t enginepower;//max pixels/second lift
   uint8_t cargobay;
   uint16_t cargocapacity;//in cubic feet
   uint8_t cargo[18];//each element represents how many of a certain type
   //for example, element 0 represents coal, which is the second item in the enum
   theplayer()
   {
      acceleration=0;
      depth_temp=0;
      drillmax=0;
      drillcount=0;
      animationcount=0;
      is_active=0;
      direction=right;
      depth=0;
      position_x=100;
      hull=1;
      hullhealth=10;
      drill=1;
      drillspeed=10;
      fueltank=1;
      fuel=150;
      engine=1;
      enginepower=3;
      cargobay=1;
      cargocapacity=10;
      rect_player_centered.x=156;//player's position drawn on the screen, always centered
      rect_player_centered.y=106;
      rect_player_centered.w=28;
      rect_player_centered.h=28;
      rect_player_animation_source.x=0;
      rect_player_animation_source.y=0;
      rect_player_animation_source.w=28;
      rect_player_animation_source.h=28;
      for(int i=0;i<15;i++)
      {
         cargo[i]=0;
      }
   }
   void sell_cargo()
   {
      *money+=30*cargo[0];//coal is worth 30
      *money+=50*cargo[1];//iron is worth 50
      *money+=80*cargo[2];//tin is worth 80
      *money+=120*cargo[3];//copper
      *money+=200*cargo[4];//silver
      *money+=400*cargo[5];//gold
      *money+=750*cargo[6];//platinum
      *money+=1000*cargo[7];//topaz
      *money+=1200*cargo[8];//sapphire
      *money+=2000*cargo[9];//emerald
      *money+=3500*cargo[10];//ruby
      *money+=5000*cargo[11];//opal
      *money+=8000*cargo[12];//jade
      *money+=20000*cargo[13];//diamond
      *money+=10000*cargo[14];//fossils
      *money+=15000*cargo[15];//artifacts
      *money+=100000*cargo[16];//sigsegv lol
      for(int i=0;i<15;i++)
      {
         cargo[i]=0;
      }
   }
};
void newrow(uint16_t row)
{
   chance[0]=1000/(abs(pow(row-10,1.1))+50);//1000/(abs(pow(row-row_where_it_is_most_common,range_of_how_common_it_is))+100/percent_of_blocks_in_most_common_row)
   //coal is found with a frequency of 100/50=2% of blocks in the row in which it is most commonly found
   //coal is most commonly found in row 10 (actually 11 in the array, but 1 more or less doesn't make a huge difference)
   //coal is commonly found in a large range of blocks, note the low exponent
   //note that 0 in this array corresponds to 2 in blocktype
   chance[1]=1000/(abs(pow(row-30,1.1))+60);//iron is found in 1.667% or 16/1000 blocks in the generator at max, and is also very common
   chance[2]=1000/(abs(pow(row-50,1.1))+80);
   chance[3]=1000/(abs(pow(row-80,1.1))+20);
   chance[4]=1000/(abs(pow(row-110,1.3))+100);
   chance[5]=1000/(abs(pow(row-150,1.5))+150);
   for(int i=0;i<40;i++)
   {
      tempint=rand()%1000;
      for(int z=0;z<7;z++)
      {
         if(z==6)
         {
            if(rand()%8)
            {
               map[row][i]=dirt;
               break;
            }
            map[row][i]=nothing;
            break;
         }
         if(chance[z]>tempint)
         {
            map[row][i]=(static_cast<blocktype>(z+2));
            break;
         }
         tempint-=chance[z];
      }   
   }
}
int main()
{
   srand(time(NULL));
   uint8_t intlength;
   SDL_Init(SDL_INIT_VIDEO);
   SDL_Surface *screen=SDL_SetVideoMode(320,240,16,SDL_SWSURFACE);
   nSDL_Font *font=nSDL_LoadFont(NSDL_FONT_VGA,0,64,0);
   SDL_Surface *sprite_blocks=nSDL_LoadImage(image_blocks);
   SDL_Surface *sprite_player=nSDL_LoadImage(image_player);
   SDL_SetColorKey(sprite_blocks,SDL_SRCCOLORKEY,SDL_MapRGB(sprite_blocks->format,0xff,0xff,0xff));
   SDL_SetColorKey(sprite_player,SDL_SRCCOLORKEY,SDL_MapRGB(sprite_blocks->format,0xff,0xff,0xff));
   SDL_Rect rect_fullscreen{0,0,320,240};
   SDL_Rect rect_sky{0,0,320,0};
   theplayer player;
   SDL_Rect sprite_blocks_source{0,0,20,20};
   SDL_Rect rect_blocks;
   rect_blocks.w=20;
   rect_blocks.h=20;
   SDL_Rect rect_hud1{0,0,320,20};
   SDL_Rect rect_hud2{0,20,20,220};
   SDL_Rect rect_blank{0,0,20,20};
   for(int i=0;i<600;i++)
   {
      newrow(i);
   }
   while(1)
   {
      if(!player.is_active)
      {
         if(isKeyPressed(KEY_NSPIRE_UP))
         {
            player.direction=up;
            player.is_active=1;
         }
         else if(isKeyPressed(KEY_NSPIRE_LEFT) && player.position_x>19)
         {
            player.direction=left;
            player.is_active=1;
            player.position_x_temp=player.position_x;
            player.drillmax=player.depth/20+200;
         }
         else if(isKeyPressed(KEY_NSPIRE_DOWN) && player.depth>-1)
         {
            player.depth_temp=player.depth;
            player.direction=down;
            player.is_active=1;
            player.drillmax=player.depth/20+200;
         }
         else if(isKeyPressed(KEY_NSPIRE_RIGHT) && player.position_x<619)
         {
            player.direction=right;
            player.position_x_temp=player.position_x;
            player.is_active=1;
            player.drillmax=player.depth/20+200;
         }
      }
      else
      {
         if(player.direction==down)
         {
            player.depth=player.depth_temp+20*player.drillcount/player.drillmax;
         }
         else if(player.direction==right)
         {
            player.position_x=player.position_x_temp-player.drillcount/player.drillmax;
         }
         else if(player.direction==left)
         {
            player.position_x=player.position_x_temp+20*player.drillcount/player.drillmax;
         }
         player.animationcount++;
         player.animationcount%=4;
         player.rect_player_animation_source.x=static_cast<currentdirection>(player.direction)*112+player.animationcount*28;
         player.drillcount+=player.drillspeed;
         if(player.drillcount>=player.drillmax)
         {
            if(player.direction==left)
            {
               player.position_x=player.position_x_temp-20;
            }
            else if(player.direction==right)
            {
               player.position_x=player.position_x_temp+20;
            }
            map[player.depth_temp/20-1][player.position_x/20+15]=nothing;
            if(player.direction==down)
            {
               player.depth=player.depth_temp+20;
            }
            player.drillcount=0;
            player.is_active=0;
         }
      }
      if(player.direction==up)
      {
         if(!isKeyPressed(KEY_NSPIRE_UP))
         {
            if(player.acceleration!=0)
            {
               player.acceleration--;
            }
            else
            {
               player.is_active=0;
            }
         }
         else if(player.acceleration!=player.enginepower)
         {
            player.acceleration++;
         }
         player.depth-=player.acceleration;
      }
      if(isKeyPressed(KEY_NSPIRE_ESC))
      {
         SDL_Quit();
         return 0;
      }
      SDL_FillRect(screen,&rect_fullscreen,0x7b08);
      if(player.depth<140)
      {
         rect_sky.h=140-player.depth;
         SDL_FillRect(screen,&rect_sky,0x9edd);
      }
      for(int z=0;z<13;z++)
      {
         if(player.depth+20*z<120)
         {
            continue;
         }
         for(int i=0;i<17;i++)
         {
            if(player.position_x/20+i<0)
            {
               continue;
            }
            rect_blocks.y=20*z-player.depth%20+10;
            rect_blocks.x=player.position_x%20+20*i;
            if(static_cast<blocktype>(map[player.depth/20+z-7][player.position_x/20+i+7])>1)
            {
               sprite_blocks_source.x=20;
               SDL_BlitSurface(sprite_blocks,&sprite_blocks_source,screen,&rect_blocks);
            }
            sprite_blocks_source.x=20*(static_cast<blocktype>(map[player.depth/20+z-7][player.position_x/20+i+7]));
            SDL_BlitSurface(sprite_blocks,&sprite_blocks_source,screen,&rect_blocks);
         }
      }
      SDL_BlitSurface(sprite_player,&player.rect_player_animation_source,screen,&player.rect_player_centered);
      //SDL_FillRect(screen,&rect_fullscreen,35362);//brown in case you were wondering-crude graphics for dirt
      SDL_FillRect(screen,&rect_hud1,0xffff);
      SDL_FillRect(screen,&rect_hud2,0xffff);
      intlength=getlength(abs(player.depth));
      shownumber(screen,abs(player.depth),intlength);
      if(player.depth<0)
      {
         for(int i=0;i<10;i++)
         {
            nSDL_SetPixel(screen,305-intlength*12+i,7,score_color);
         }
      }
      SDL_Flip(screen);
   }
   return 0;
}
(code still needs to be cleaned up a lot)
Probably some other derp fail, but was wondering
You can view the glitch in the attachment




(gcc occasionally glitches for me in codeblocks (the exact same code compiles to different things at different times) because my computer is weird, but I've tried this lots of times)
Title: Re: good random numbers
Post by: Vogtinator on June 25, 2014, 11:39:33 am
:o To be honest, your source code is awful:

I'd advise you to read a good c++ book or online tutorials, but it'll take a while.
Title: Re: good random numbers
Post by: Sorunome on June 25, 2014, 11:40:55 am
Why shouldn't one include cpp files?
Title: Re: good random numbers
Post by: Vogtinator on June 25, 2014, 11:43:14 am
They will also get picked up by the linker and there may be some "duplicate definition of symbol" errors.
Also, you wouldn't be able to use it in two seperate files, as the symbols in it would then cause the same issue.
If there aren't any symbols in it, it doesn't need to get compiled -> header file.
Title: Re: good random numbers
Post by: kevinkore3 on June 25, 2014, 09:48:13 pm
I know my source code kind of sucks. I used getlength because people said logs were lots slower (dunno if that's true, but it had lots of thumbs ups and I'm not taking risks at 132mhz) I know there are lots of bigger things to optimize, but this was an easier one to do
As for the 1* and 1.1*, I was also thinking that it should give the same result, but it didn't. As for the money thingy,  I have no idea why I put it there :P  probably wasn't thinking (do I need any pointers?)
For the cargo thingy, there is no pattern to how much each item costs, so I decided against a loop.
I will clean up the source after I finish.
Anyone have any idea about the weird multiplying glitch? If I do +=40 it gets jerky.
I'm happy that it runs and gets decent fps at least  :D
Thanks for the suggestions