Pages: [1]   Go Down
Author Topic: randomSeed(analogRead(0));  (Read 1726 times)
0 Members and 1 Guest are viewing this topic.
india
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i'm using video experimenter shield for arduino. I have downloaded a program for dispalying text on tv screen from  nootropic design website. the program contains  " randomSeed(analogRead(0)); " this function. i have gone through the arduino website where they have clearly mentioned about the advantages of using this function. My doubt is the random number generated by this function is to assigned to what ??? wheteher the value generated by this function is used by the computer as the location for a particular memory ??? and why this function has to be declared in void setup(() but not in void loop() ????

can anybody help please ????
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25763
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That function just seeds the random number generator - it doesn't create a random number.

Is there something wrong with your question-mark key?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3221
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That function defines where the seed for the random number generator is picked up from. In this case, it will be from analog pin 0, which has to be left floatingfor this to make some sense. Since the pin is floating, the value that will seed the random number generator will never be the same thus making it truly random.

It needs to be done only one time, since it will only define where the seed comes from and not generate actual numbers.

Try looking for random number in C, the explanation is good enough and applicable in Arduino too.
Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

india
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the replies...and sorry for that question marks
Logged

Dallas, Texas
Offline Offline
God Member
*****
Karma: 30
Posts: 887
Old, decrepit curmugeon
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

While it is common practice to seed the internal random generator this way, it doesn't really do a very good job.  In practice, the analogRead(0) on an unconnected pin doesn't produce too many possible values, and even theoretically will only produce up to 10 bit (in practice much less), when the seed function is designed to use a 32-bit integer.

Here are some examples of what the randomSeed(analogRead(0)) used on an example Arduino;
  // The more normal randomSeed(analogRead(0)) produces far less random seed
  // values as showm in the following 25 examples:
  // Seed value = 303
  // Seed value = 326
  // Seed value = 327
  // Seed value = 326
  // Seed value = 326
  // Seed value = 328
  // Seed value = 328
  // Seed value = 328
  // Seed value = 330
  // Seed value = 328
  // Seed value = 328
  // Seed value = 329
  // Seed value = 327
  // Seed value = 328
  // Seed value = 328
  // Seed value = 329
  // Seed value = 329
  // Seed value = 329
  // Seed value = 331
  // Seed value = 331
  // Seed value = 330
  // Seed value = 331
  // Seed value = 329
  // Seed value = 329
  // Seed value = 329


As you can see, in 25 samples, there are only seven different values, which differ by only a couple of low end bits...
Logged

New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 1.0.1

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3221
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Still better than randomSeed(3); ...

Any ideas on how to make it better?
Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 246
Code Monkey
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Still better than randomSeed(3); ...

Any ideas on how to make it better?

Search this forum for PRNG and PRNG seeders. There have been some interesting discussions, and lots of pointers to code that might give you better randomness.

... if you need it. Seeding the PRNG with the current date is a terrible thing, but it is a reasonably terrible thing to do if all you need is a reasonably different set of PRNs for each invocation. And you can reseed the PRNG over the lifetime of the program, if you need to select a reasonably different set of PRNs.

But if you need better PRNs, you can get them. You almost certainly need to use a third-party library.
Logged

I yield() for co-routines.

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 198
Posts: 12747
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Still better than randomSeed(3); ...

Wanna bet?

Quote
Any ideas on how to make it better?

There are two approaches.  One is to use a reliable source of entropy like @wanderson's library to generate a seed....
http://arduino.cc/forum/index.php/topic,108380.0.html

The other is to save the seed and apply a reasonable algorithm for advancing the value on the next reset...
http://arduino.cc/forum/index.php/topic,66206.0.html
Logged

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3221
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the links. smiley
Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

india
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for the replies...was very much helpful
Logged

Ontario
Offline Offline
God Member
*****
Karma: 24
Posts: 860
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Another thread, with links to still more: http://arduino.cc/forum/index.php/topic,71841

On there you will find some potentially useful code to swizzle together a whole slew of analogue readings to get something with more entropy in it.  It's still taking a chance that analogue reads of floating pins have useful noise on them, and that is simply not a completely safe assumption.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it necessary for this function randomSeed(analogRead(0)); to be preceded by
Serial.begin(9600); as seen here...

http://arduino.cc/en/Reference/RandomSeed

In short, is the analogpin 0 affected by Serial.begin?
Thanks.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25763
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

They are completely independent.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
As you can see, in 25 samples, there are only seven different values, which differ by only a couple of low end bits...

There are quite a few ways to get around that.

But the approach fails fundamentally because the stock analogRead() is incorrectly coded.
Logged

Pages: [1]   Go Up
Jump to: