Difference between revisions of "Gb.pickRandomSeed"
From Gamebuino Wiki
(Explanation of the function) |
(Added example) |
||
Line 31: | Line 31: | ||
== 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 == |
Revision as of 2014-08-04T22:41:15
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 615,970 to 4,293,588,651. (Achieved using a fully charged Gamebuino)
Syntax
gb.pickRandomSeed();
Parameters
none
Returns
none
Example
/* * 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); } } }