Author Topic: good random numbers  (Read 12231 times)

0 Members and 1 Guest are viewing this topic.

Offline kevinkore3

  • LV3 Member (Next: 100)
  • ***
  • Posts: 57
  • Rating: +0/-0
    • View Profile
good random numbers
« 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];
}
}
}

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: good random numbers
« Reply #1 on: June 17, 2014, 05:25:48 pm »
Shouldn't your language have pseudo-random built-in?

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline kevinkore3

  • LV3 Member (Next: 100)
  • ***
  • Posts: 57
  • Rating: +0/-0
    • View Profile
Re: good random numbers
« Reply #2 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

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: good random numbers
« Reply #3 on: June 17, 2014, 05:26:52 pm »
you only set the seed once all at the beginning of your program.

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: good random numbers
« Reply #4 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.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline kevinkore3

  • LV3 Member (Next: 100)
  • ***
  • Posts: 57
  • Rating: +0/-0
    • View Profile
Re: good random numbers
« Reply #5 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
« Last Edit: June 17, 2014, 05:35:35 pm by kevinkore3 »

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: good random numbers
« Reply #6 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 :)

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Streetwalrus

  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3821
  • Rating: +80/-8
    • View Profile
Re: good random numbers
« Reply #7 on: June 17, 2014, 06:00:15 pm »
Havege. :trollface:
Nah jk. There are good prng algorithms out there. Google around Wikipedia. :P

Offline bb010g

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 428
  • Rating: +22/-1
  • I do stuff
    • View Profile
    • elsewhere on the net
Re: good random numbers
« Reply #8 on: June 17, 2014, 06:21:01 pm »
Arch Linux user
Haskell newbie | Warming up to Lua | Being dragged into C++
Calculators: HP 50g, HP 35s, Casio Prizm, TI-Nspire CX CAS, HP 28s, HP Prime, Mathematica 9 (if that counts)
π: 3.14...; l: 108; i: 105; e: 101; l+i+e: 314
THE CAKE IS A LIE IS A PIE

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: good random numbers
« Reply #9 on: June 17, 2014, 06:43:05 pm »
xkcd always does it ;)

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: good random numbers
« Reply #10 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

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline pimathbrainiac

  • Occasionally I make projects
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1731
  • Rating: +136/-23
  • dagaem
    • View Profile
Re: good random numbers
« Reply #11 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.
I am Bach.

Offline kevinkore3

  • LV3 Member (Next: 100)
  • ***
  • Posts: 57
  • Rating: +0/-0
    • View Profile
Re: good random numbers
« Reply #12 on: June 19, 2014, 11:08:47 pm »


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


Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: good random numbers
« Reply #13 on: June 19, 2014, 11:30:22 pm »
Looks nice so far by the way :D

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: good random numbers
« Reply #14 on: June 20, 2014, 12:27:18 am »
Looks nice so far by the way :D
Yup, looking nice!

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!