Difference between revisions of "Gb.pickRandomSeed"
From Gamebuino Wiki
(Added example) |
m (Spelling) |
||
Line 6: | Line 6: | ||
The function is defined in the library as follows: | The function is defined in the library as follows: | ||
− | < | + | <pre> |
void Gamebuino::pickRandomSeed(){ | void Gamebuino::pickRandomSeed(){ | ||
− | + | randomSeed(battery.voltage * ~micros() + backlight.ambientLight + micros()); | |
} | } | ||
− | </ | + | </pre> |
The range of values of each part of the function are: | The range of values of each part of the function are: | ||
Line 19: | Line 19: | ||
This allows seed values ranging from 0 to 4294964116 (for an unsigned long). | This allows seed values ranging from 0 to 4294964116 (for an unsigned long). | ||
− | Realistically, values are expected to be in the range of | + | Realistically, values are expected to be in the range of 23,761 to 4,293,588,651. (Achieved using a fully charged Gamebuino) |
== Syntax == | == Syntax == | ||
Line 33: | Line 33: | ||
<pre> | <pre> | ||
/* | /* | ||
− | * | + | * pickRandomSeed function example - By Jack Burgess (aka Matrix828) |
* | * | ||
− | * A demo that performs the gb. | + | * A demo that performs the gb.pickRandomSeed() function and displays the result, |
* with the highest and lowest generated values shown as well. | * with the highest and lowest generated values shown as well. | ||
* | * | ||
Line 50: | Line 50: | ||
void setup() { | void setup() { | ||
gb.begin(); | gb.begin(); | ||
− | gb.titleScreen(F(" | + | gb.titleScreen(F("pickRandomSeed Example")); |
} | } | ||
Revision as of 2014-08-04T22:44:24
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.
The function is defined in the library as follows:
void Gamebuino::pickRandomSeed(){ randomSeed(battery.voltage * ~micros() + backlight.ambientLight + micros()); }
The range of values of each part of the function are:
-
battery.voltage
: 4200 to 3500 -
~micros()
: -1 to -4,294,967,293 -
backlight.ambientLight
: 0 to 1024 -
micros()
: 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 23,761 to 4,293,588,651. (Achieved using a fully charged Gamebuino)
Syntax
gb.pickRandomSeed();
Parameters
none
Returns
none
Example
/* * pickRandomSeed function example - By Jack Burgess (aka Matrix828) * * A demo that performs the gb.pickRandomSeed() 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("pickRandomSeed 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); } } }