I don't understand gb.pickRandomSeed()

Understanding the language, error messages, etc.

I don't understand gb.pickRandomSeed()

Postby noah » Tue Aug 09, 2016 8:17 pm

Hi,
Just wondering, what does gb.pickRandomSeed() actually do? Like it's in all the examples with the explanation 'to make each game different' but how does it work, what can it actually change in Pong etc, and why should I put it in my projects?

I just want to clear it up.

Thanks in advance, noah :D
User avatar
noah
 
Posts: 25
Joined: Mon Jul 25, 2016 8:32 am
Location: Spiffin' Britain

Re: I don't understand gb.pickRandomSeed()

Postby naed » Tue Aug 09, 2016 8:59 pm

it allows you to pick a random number based on battery voltage, the ambient light sensor and the time elapsed since start up.

it's there if you need something to be random, in one of my games i have items shooting from the side of the screen, if this was programmed in a sequence you could possibly memorise the pattern and it wouldn't make for good gameplay, if i use gb.pickRandomSeed() it can help make this truly random
User avatar
naed
 
Posts: 140
Joined: Tue May 31, 2016 3:18 pm

Re: I don't understand gb.pickRandomSeed()

Postby Sorunome » Wed Aug 10, 2016 10:57 am

A computer can't create real random numbers just by calculations, which is an issue if you want to have random events in e.g. a video game (thsi is also an issue in cryptography but I won't get into that here).
So, instead of generating so-called true random the computer (and the gamebuino) generates pseudo-rando. As the name already suggests, this randomness isn't really random, instead it is some carefully modeled function where you put numbers in, somehow like this:
Code: Select all
x0 = f(seed)
x1 = f(x0)
x2 = f(x1)
etc. so the "random" function feeds off of the previous result. As easily seen, if the seed is the same then the sequence of random numbers that follows is the same, this is why you try to pick an as-random-seed as possible. Possible random sources are things like user-input, as it is e.g. impossible to predict just how many microseconds the user will be on the start-menu screen. Other such sources are the battery voltage and the light sensor. Calling gb.pickRandomSeed() will seed this random number generator with some maths operation of these values (as seen here).

Now, as the random number generator of the arduino is 32-bit, that means it has a period of 2^32, or 4294967296. That means that you need to generate that many random numbers until they will actually re-occor in the same order, which is quite unlikely (for games!!!). And also picking the random seed can almost guarantee that you will get a different sequence of random numbers every time you run your game.

So, what does that mean to you? If you want to use good random numbers just call gb.pickRandomSeed(); after your gb.titleScreen(); call. (This recomendation is also stated in the reference)
Last edited by Sorunome on Wed Aug 10, 2016 5:04 pm, edited 1 time in total.
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm

Re: I don't understand gb.pickRandomSeed()

Postby noah » Wed Aug 10, 2016 4:17 pm

Right, so if I wanted a sprite to randomly appear on the screen, how would I do that? Do you only have to generate one random number for everything?
-noah
User avatar
noah
 
Posts: 25
Joined: Mon Jul 25, 2016 8:32 am
Location: Spiffin' Britain

Re: I don't understand gb.pickRandomSeed()

Postby Sorunome » Wed Aug 10, 2016 5:03 pm

You just have to call gb.pickRandomSeed() once after gb.titleScreen() and then you can use the build-in random functions for arduino just normally.
Such as random() would work then to generate random numbers for you. However, don't forget to do the gb.pickRandomSeed() (However, don't run that every time when you want to generate a random number, just once after gb.titleScreen())!
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 28 guests

cron