Problem with rand() function please help

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi folks :)

I know that the subject was discussed many times but I have a problem to implement random function.
If you could check what am I doing wrong I would be really gratefull

Let me before explain what I want to achive

I want to make a random number from 0 to 3 each time.
I am taking new seed from ADC input Actually it is connected to a microphone.

Below I pasted code:

//declarations

#include
unsigned char choose_lamp();


//function

unsigned char choose_lamp()
{
unsigned char seedn;

seedn=ADC_read(0); //read really random value (take a seed from microphone )
srand(seedn);      // setting new seed for rand() function
seedn=(rand()%3);		 

return seedn; // shuld return value from 0 to 3
}

What am I doing wrong please help

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you call this function many times in the short period of time, you tend to re-initialise the random with the same value. You shouls initialise the random once and then just call the rand() function. It is not re-entrant, so all calls to rand()affect the same internal static seed value.

Debugging is for sissies and delivery for surgeons. Real men do demonstration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What doesn't work exactly?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well it is choosing all the time the same value !
I rewrote the function and now I don`t give each time a new seed but it didnt help ! All the time the same value :/

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And how sure are you that ADC_read(0); is returning a "random" value when you seed?

Remeber that the way a PRNG works is that it's just an algorithm that will output a sequence of numbers in an "odd" order but the order will always be the same.

1, 2, 3, 4, 5, 6, 7, 8, 9

maybe becomes

4, 9, 1, 3, 2, 7, 8, 6, 5

then when you "seed" you are saying "start giving me the numbers from position "seed" onwards". So say you seed with 3 then you get a sequence of numbers it's guaranteed you'll get 1, 3, 2, 7, 8, 6, 5, 4, 9, 1, 3....

If you then start over and see with 3 again then you'll get the same sequence. If, however, you seed with 5 you'd get 2, 7, 8, 6, 5, 4, 9, 1, 3, 2, 7...

Hopefully you get the picture (though there's more than one digit involved in the real thing of course so the number "look" more random).

It's therefore KEY that you seed with a different value each time you apply power. To do this you need some "entropy". Now the ADC is one way, another is to time in milliseconds from power on until a button pressed or a UART character received as these things are seldom accurate to the millisecond. In fact you can keep injecting "entropy" into some RNGs to help to keep things mixed up a bit. For example, on Linux, two sources of entropy that are regularly fed into the RNG are time between mouse movements and time between the reception of ethernet packets (amongst other things)

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
seedn=(rand()%3);       

return seedn; // shuld return value from 0 to 3 

Minor point but modulo 3 will give values 0 to 2. Use the AND operator instead.

C: i = "told you so";

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry for such a long delay in answering but I had some problems with my network.

When I left only rand() function everything works fine apart from that I receive each time the same sequence :) But in my project real randomness doesnt matter so much.

I was taking seed from microphone
So when music was turned off it was impossible to provide right seed !

Thanks Cliff for great explanation what for is seeding and how exactly it works !

Cupluscon when you use "rand()%3" you will receive 0,1,2 as you said
I made mistake writing comments
In project I need 0,1,2 to switch between functions.

Thanks indeed guys

Adam