Initialize random numbers generator

Understanding the language, error messages, etc.

Initialize random numbers generator

Postby FreddyBoubil » Tue May 13, 2014 2:39 pm

Hi all,

I've been working on some little progs to train me, waiting the Gamebuino. I've noticed that random function always returns the same numbers. I forgot that in C we had to initialize the random numbers generators.

I wanted to use time(), but it made me an error. I downloaded Time.h on Arduino website, and call Hours(), Minutes() and Seconds(), since time() stil doesn't work.

The problem is that these functions always return 0, I suppose because there is no time on the Gamebuino (or the emulator).

How can I initialize random numbers generator ?

Thanx
FreddyBoubil
 
Posts: 9
Joined: Fri Mar 28, 2014 1:49 pm
Location: France

Re: Initialize random numbers generator

Postby adekto » Tue May 13, 2014 6:02 pm

im not really sure how accurate the emulator is at the moment that my cause the incompatibility with the time.h also since its emulating it may repeat the same randomized pattern each time

it could also be somthing wrong with your code if u would mind sharing we might figure it out
User avatar
adekto
 
Posts: 448
Joined: Tue Feb 25, 2014 9:47 pm
Location: belgium

Re: Initialize random numbers generator

Postby Myndale » Tue May 13, 2014 9:17 pm

The time library requires external hardware like the DS1307. Reading an analog value from an unconnected pin will give you a random value that you can use as a RNG seed, although I suspect the emulator doesn't currently support this. If you want a relatively random seed then I'd suggest you make your program sit in a loop incrementing a variable while displaying a "Press any key to start" message on the display.
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

Re: Initialize random numbers generator

Postby rodot » Wed May 14, 2014 6:33 am

Arduino has two functions to measure time:
millis() returns the time elapsed since the microcontroller started in milliseconds.
micros() is the same but in microseconds, with a resolution of 4 microseconds.
But usually people read a value from from a disconnected analog input. Something like
randomSeed(analogRead(A0))
You could use the ambient light sensor as there is no disconnected analog inputs on Gamebuino.
PS: I'm not home, I'll post links when I'm back.
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: Initialize random numbers generator

Postby FreddyBoubil » Wed May 14, 2014 7:44 am

Thanks for your answers.

I realised that, whatever the value I use in srand() function, I still always have the same numbers.

Here is my code (I want random numbers between 0 and 10) :
Code: Select all
#include <SPI.h>
#include <Gamebuino.h>

Gamebuino gb = Gamebuino();
int i;
int test;
int valrnd[20];

void setup() {
  test = 10;
  srand(test);
  for (i=0; i<20; i++) {
    valrnd[i] = random(0,10);
  }
  gb.begin(F("Test"));
}

void loop() {
  if (gb.update()) {
    for (i=0; i<10; i++) {
      gb.display.setCursor(6*i,20);
      gb.display.print(valrnd[i]);
    }
    gb.display.setCursor(1,1);
    gb.display.print(test);
  }
}
FreddyBoubil
 
Posts: 9
Joined: Fri Mar 28, 2014 1:49 pm
Location: France

Re: Initialize random numbers generator

Postby Myndale » Wed May 14, 2014 8:21 am

FreddyBoubil wrote:I realised that, whatever the value I use in srand() function, I still always have the same numbers.


srand() is meant to be used with rand(), which is a standard C library function that returns a random number between 0 and RAND_MAX. The random() function that you're using is part of the Arduino libraries, so use randomSeed() instead:

http://arduino.cc/en/reference/random
http://arduino.cc/en/Reference/RandomSeed
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

Re: Initialize random numbers generator

Postby FreddyBoubil » Wed May 14, 2014 10:16 am

Thank you, indeed it works greater :)

I tried to use millis() and micros(). millis() always returns 0 and micros() always returns 4. For ambient light sensor, I don't know if the emulator handles it.
FreddyBoubil
 
Posts: 9
Joined: Fri Mar 28, 2014 1:49 pm
Location: France

Re: Initialize random numbers generator

Postby FreddyBoubil » Mon May 19, 2014 12:08 pm

Hi all,

I tried to use gb.battery.getVoltage() and getLevel() but both return 0, because of the emulator I suppose. So for now I'll have to use the solution given by Myndale I think :

Myndale wrote:If you want a relatively random seed then I'd suggest you make your program sit in a loop incrementing a variable while displaying a "Press any key to start" message on the display.
FreddyBoubil
 
Posts: 9
Joined: Fri Mar 28, 2014 1:49 pm
Location: France

Re: Initialize random numbers generator

Postby Myndale » Mon May 19, 2014 10:25 pm

The main problem with my method is that the user will probably press the button within a second or so, and since the Gamebuino library refresh rate is 60 you'll only get ~60 unique starting values, and statistically most values will probably be around the middle 20 or so. You can significantly improve upon this by saving your seeds to EEPROM so that they're different each time Gamebuino is started up.

An even better method would be to initialize one of the timers to count up and set the prescalar to 1, then make one of the buttons generate an external interrupt and read the counter value inside the interrupt handler. That will give you 16 million possible values per second, and when combined with the EEPROM technique above it should for most purposes give you a fairly acceptable rng.
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

Re: Initialize random numbers generator

Postby FreddyBoubil » Thu May 22, 2014 10:25 am

Myndale wrote:The main problem with my method is that the user will probably press the button within a second or so, and since the Gamebuino library refresh rate is 60 you'll only get ~60 unique starting values, and statistically most values will probably be around the middle 20 or so. You can significantly improve upon this by saving your seeds to EEPROM so that they're different each time Gamebuino is started up.

An even better method would be to initialize one of the timers to count up and set the prescalar to 1, then make one of the buttons generate an external interrupt and read the counter value inside the interrupt handler. That will give you 16 million possible values per second, and when combined with the EEPROM technique above it should for most purposes give you a fairly acceptable rng.


Thank you Myndale, your ideas look great and I like them. The main problem is that program them is above my knowledge :) I'll wait for the references guide that rodot currently writes to see if I'll have my answers in them. Or maybe should I look in Arduino guides ?
FreddyBoubil
 
Posts: 9
Joined: Fri Mar 28, 2014 1:49 pm
Location: France

Next

Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 13 guests

cron