This is driving me mad...
In my main program file:
randomSeed(analogRead(0));
followed by calls to:
random(0,4);
always produces random numbers for me.
However if I do the same code inside a class in a different file then I get the same numbers every time, no matter what value I enter in randomSeed().
Is there a logical reason for this?
THanks,
Tom
Could you be ending up with the avrlib version of random() rather than the arduino version? I'm not quite sure how that would happen, but slightly different compile steps happen for .pde vs .cpp files...
Could you be ending up with the avrlib version of random() rather than the arduino version? I'm not quite sure how that would happen, but slightly different compile steps happen for .pde vs .cpp files...
Possibly? I don't know... How would I investigate?
Could you be ending up with the avrlib version of random() rather than the arduino version?
I don't think it's possible. The avrlib version is named "_random" while the Arduino version is some long strange C++ mangled name.
In addition, the avrlib version doesn't have any parameters. A compilation error would result if random was the one defined in stdlib.h.
Is there a logical reason for this?
Absolutely. Computers don't know how to do things that are illogical. (except, maybe, when the math coprocessor is fried)
But there isn't enough information to know what the logical reason is. Post your Sketch.
This is the basic idea, but every sequence is identical, no matter what is passed into randomSeed():
#include "WProgram.h"
class CRandSeq
{
public:
CRandSeq( const int size, const int minNum, const int maxNum );
~CRandSeq( void );
void CreateSequence( void );
unsigned int GetSize( void ) { return m_maxSeq; };
long GetElement( const int pos ) { return m_sequence[pos]; };
private:
int m_maxSeq;
int m_minNum;
int m_maxNum;
long* m_sequence;
};
#include "CRandSeq.h"
CRandSeq::CRandSeq( const int size, const int minNum, const int maxNum )
{
randomSeed(analogRead(0));
m_maxSeq = size;
m_minNum = minNum;
m_maxNum = maxNum;
m_sequence = (long*) malloc (m_maxSeq);
if(m_sequence == 0)
{
// failed allocation -> do something!?
}
// create the random sequence
CreateSequence();
}
CRandSeq::~CRandSeq( void )
{
// free the allocated memory
if(m_sequence != 0)
{
free(m_sequence);
m_sequence = NULL;
}
}
void CRandSeq::CreateSequence( void )
{
for(int i = 0; i < m_maxSeq; i++)
m_sequence[i] = random(m_minNum, m_maxNum);
}
#include "CRandSeq.h"
CRandSeq Seq( 64, 0, 4 );
void setup( void )
{
Serial.begin(9600);
for( int i = 0; i < 64; i++ )
Serial.println( Seq.GetElement( i ) );
}
void loop( void )
{
}
system
May 20, 2010, 12:01pm
7
randomSeed(analogRead(0));
When your constructor is called, the analog pins have not been initialized yet. So, you will always get the same value.
This results in the same sequence of random numbers.
Add a begin method to your class, move the randomSeed call to the begin method, and call the begin method in setup.
See if that improves the randomness of the values.
When your constructor is called, the analog pins have not been initialized yet. So, you will always get the same value.
This results in the same sequence of random numbers.
Ahh. Thanks very much. I will test this evening and let you know the outcome.
PaulS - Top job mate. Works beautifully now