Calculator Community > Calculator C

good random numbers

(1/4) > >>

kevinkore3:
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: ---
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];
}
}
}

--- End code ---

Sorunome:
Shouldn't your language have pseudo-random built-in?

kevinkore3:

--- Quote from: Sorunome on June 17, 2014, 05:25:48 pm ---Shouldn't your language have pseudo-random built-in?

--- End quote ---
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

Sorunome:
you only set the seed once all at the beginning of your program.

AngelFish:
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.

Navigation

[0] Message Index

[#] Next page

Go to full version