Difference between revisions of "Gb.pickRandomSeed"

From Gamebuino Wiki
Jump to: navigation, search
(Added example)
m (Reverted edits by Matrix828 (talk) to last revision by Rodot)
 
(One intermediate revision by one other user not shown)
Line 3: Line 3:
 
== Description ==
 
== Description ==
 
Picks a random seed using a mix of the battery voltage, ambient light sensor and the time elapsed since start up. It should be placed right after <code>gb.begin()</code> and <code>gb.titleScreen()</code>, this way the random seed will depend on how long the user takes to press "A" to leave the title screen.
 
Picks a random seed using a mix of the battery voltage, ambient light sensor and the time elapsed since start up. It should be placed right after <code>gb.begin()</code> and <code>gb.titleScreen()</code>, this way the random seed will depend on how long the user takes to press "A" to leave the title screen.
 
The function is defined in the library as follows:
 
 
<code>
 
void Gamebuino::pickRandomSeed(){
 
:randomSeed(battery.voltage * ~micros() + backlight.ambientLight + micros());
 
}
 
</code>
 
 
The range of values of each part of the function are:
 
* <code>battery.voltage</code> : 4200 to 3500
 
* <code>~micros()</code> : -1 to -4,294,967,293
 
* <code>backlight.ambientLight</code> : 0 to 1024
 
* <code>micros()</code> : 0 to 4,294,967,292
 
 
This allows seed values ranging from 0 to 4294964116 (for an unsigned long).
 
Realistically, values are expected to be in the range of 615,970 to 4,293,588,651. (Achieved using a fully charged Gamebuino)
 
  
 
== Syntax ==
 
== Syntax ==
Line 31: Line 14:
  
 
== Example ==
 
== Example ==
<pre>
 
/*
 
* RandomSeed function example - By Jack Burgess (aka Matrix828)
 
*
 
* A demo that performs the gb.randomSeed() function and displays the result,
 
* with the highest and lowest generated values shown as well.
 
*
 
*/
 
 
#include <SPI.h>
 
#include <Gamebuino.h>
 
Gamebuino gb;
 
 
unsigned long seed;
 
unsigned long highest = 0;
 
unsigned long lowest = 429130353; // Random high value
 
 
void setup() {
 
    gb.begin();
 
    gb.titleScreen(F("RandomSeed Example"));
 
}
 
 
void loop(){
 
    while (1){
 
        if(gb.update()){
 
            seed = gb.battery.voltage * ~micros() + gb.backlight.ambientLight + micros();
 
            if (seed > highest) {
 
                highest = seed;
 
            } else if (seed < lowest) {
 
                lowest = seed;
 
            }
 
            gb.display.print(F("Highest:"));
 
            gb.display.println(highest);
 
            gb.display.print(F("Lowest: "));
 
            gb.display.println(lowest);
 
            gb.display.print(F("Seed:  "));
 
            gb.display.println(seed);
 
 
        }
 
    }
 
}
 
</pre>
 
  
 
== See also ==
 
== See also ==

Latest revision as of 2014-08-04T23:39:30

Description

Picks a random seed using a mix of the battery voltage, ambient light sensor and the time elapsed since start up. It should be placed right after gb.begin() and gb.titleScreen(), this way the random seed will depend on how long the user takes to press "A" to leave the title screen.

Syntax

gb.pickRandomSeed();

Parameters

none

Returns

none

Example

See also