### Author Topic: random numbers  (Read 3200 times)

0 Members and 1 Guest are viewing this topic.

#### ross75

• Jr. Member
• Posts: 9
##### random numbers
« on: November 10, 2008, 09:15:27 PM »
How can i get a random number within a range of numbers?
I'd like to make a function and if possable seed the generator with a unpredictable number from a clock or somthing like that so i dont have the sequence repeat.

for example

int randomnumber(int MinVal , int MaxVal)
{
int Number;
Number = generate random number between MinVal and MaxVal;

return Number;
}

#### Webbot

• Expert Roboticist
• Supreme Robot
• Posts: 2,166
##### Re: random numbers
« Reply #1 on: November 10, 2008, 11:52:03 PM »
Of course you colud use the C rand() function but that has the side effect of bringing in the floating point library. Which is fine if you want floating point random numbers.

If you only want integers then heres an excerpt from my text to speech synthesizer tutorial:

Code: [Select]
// Random number seed
uint8_t seed0=0xecu;
uint8_t seed1=7;
uint8_t seed2=0xcfu;

/*
Generate a random number from 0 to 255
*/
uint8_t random(void){
uint8_t tmp = (seed0 & 0x48) + 0x38;
seed0<<=1;
if(seed1 & 0x80){
seed0++;
}
seed1<<=1;
if(seed2 & 0x80){
seed1++;
}
seed2<<=1;
if(tmp & 0x40){
seed2++;
}
return seed0;
}

This will only work if you want numbers in the range 0 to 255 or less.

If you want to generate numbers from a subset of this then you could add:-
Code: [Select]
uint8_t rnd(uint8_t minVal, uint8_t maxVal){
unit8_t range = maxVal - minVal + 1;
return  (random() % range) + minVal;

}

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

#### sdavis6736

• Jr. Member
• Posts: 26
##### Re: random numbers
« Reply #2 on: November 11, 2008, 04:14:38 AM »
Have you thought about using fuzzy logic?  Check out http://www.fuzzytech.com/ It is a pretty interesting program.  It costs a pretty penny, but it might give you an idea or two of how to implement something more random than what is already out there.

#### ross75

• Jr. Member
• Posts: 9
##### Re: random numbers
« Reply #3 on: November 11, 2008, 02:27:46 PM »
Thanx for the help
what header files do i need to #include to use this code?

could i hook up a photoresistor or somthing like that to one of the inputs of the mcu and use its reading as a seed every time my random function is called? That would give a good random seed right?
for example:

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
int GetRnd( int min , int max)// initalize the function

int GetRnd( int min , int max)// random number function
{
int MyRandomNumber;// the random number
int Seed;// the seed used to generate the random number
Seed = (PINB & 0b0001000);// read the value of pinb bit 3
srand(Seed);// seed the random number generator
MyRandomNumber = rand() % max + min; generate the random number with in a given range
return MyRandomNumber;
}

Wouldn't this return a vlaue between 5 and 50 with a good random seed?
« Last Edit: November 11, 2008, 03:04:52 PM by ross75 »

#### Webbot

• Expert Roboticist
• Supreme Robot
• Posts: 2,166
##### Re: random numbers
« Reply #4 on: November 11, 2008, 09:17:48 PM »
A random number generator actually returns the next number from a long sequence of 'random' numbers that takes a long time until it repeats the sequence. The 'seed' just gives the starting point in this long sequence. So you should only call it 'once' (say at the start of your 'main') - rather than every time you ask for the next number.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

• Supreme Robot
• Posts: 11,680
##### Re: random numbers
« Reply #5 on: November 11, 2008, 10:13:28 PM »
Just as another example, my Axon beta software has a documented random number function you can look at.

#### Dscrimager

• Full Member
• Posts: 57
##### Re: random numbers
« Reply #6 on: December 19, 2008, 12:02:05 AM »
And you could do this:

int randomnumber(int MinVal , int MaxVal)
{
return (rand() % MaxVal) + MinVal;
}

You need to make sure you don't over/underflow... and you should check MinVal < MaxVal... this is where rand is returning an integer to begin with , if not you can scale and then do the magic...

Doug

#### cosminprund

• Robot Overlord
• Posts: 284
##### Re: random numbers
« Reply #7 on: December 19, 2008, 02:48:04 AM »
A random number generator actually returns the next number from a long sequence of 'random' numbers that takes a long time until it repeats the sequence. The 'seed' just gives the starting point in this long sequence. So you should only call it 'once' (say at the start of your 'main') - rather than every time you ask for the next number.

That's how the "usual" random number generators work. Good random number generators used in security applications use unpredictable external entropy generators. I can give you two examples for this: The Linux kernel includes two random numbers generators, you can see them throw the /dev/random and /dev/urandom pseudo-devices. The /dev/random generator uses entropy to give very good random numbers (this entropy comes from things like ethernet packets and other i/o activity). It can't give an infinite number of random values (if you request random numbers fast enough it will eventually run out of "entropy" and block until more entropy is acquired). The second example is from TrueCrypt OR the putty ssh suite, don't remamber exactly. When it needs to generate a random private key it asks you to move the mouse over the window to "acquire some entropy". That's very smart because you can't possibly move the mouse in the same pattern twice, so it gives good entropy.

@ross75
Using a photoresistor as a entropy generator might not be a good idea since the photoresistor might generate the same value over and over again (if the robot doesn't move from it's point, if there's too much light and the photoresistor is saturated, if there's too dark, if the light levels in the room are uniform). Unless you require unusually "random" random numbers it would also be much slower since analog to digital conversion doesn't happen in an instant.

In my opinion you should not bother "fixing" the random number generator unless it is annoyingly broke: Example: if your "robot" is dealing poker cards and allways gives 4 aces to your opponents, if your robot uses the random number generator to select a different direction to travel when it encounters an wall and allways selects the same direction.

• Supreme Robot
• Posts: 11,680
##### Re: random numbers
« Reply #8 on: December 19, 2008, 04:12:59 AM »
Quote
It can't give an infinite number of random values (if you request random numbers fast enough it will eventually run out of "entropy" and block until more entropy is acquired).
In other words, polling for the random number must be at a much lower frequency than the rate of entropy change.

One method I've seen is to use a sensor that's inherently noisy (like the Sharp IR), read it using a 10 bit ADC, and only use the very last 10th bit as the random number. Noisier the sensor the better. Just be sure to do a histogram on the likelihood of each number (0-9) to appear to verify that its truly random.

#### Webbot

• Expert Roboticist
• Supreme Robot
• Posts: 2,166
##### Re: random numbers
« Reply #9 on: December 19, 2008, 02:23:23 PM »
Sometimes you need to differentiate between 'truly random and non-repeatable' versus 'pseudo-random but repeatable'.

For the second case you can seed the random number generator to a known values and then get a series of 'random' numbers which will be the same every time you run the code. This is useful if you suspect that your robot 'makes the wrong decision' at a certain point as you can always replay and repeat what it is doing. ie great for debugging.

Whereas the first case may give a different sequence of numbers and it is beyond your control to repeat the sequence. So if the robot goes haywire then it can be hard to recreate the scenario at a later date. ie hard to debug.

Suggest that starting with the 'pseudo random but repeatable' case is the best starting point. Debug your code - then switch to a more sophisticated generator if needed.

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk