0

How to write a function and how to generate random numbers
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How to write a function and how to generate random numbers

by magician13134 on Sun Jun 01, 2008 8:04 am

Hi, I'm working on a project and I need to generate two, one digit random numbers and pass them to a function. I've seen examples of functions, like this one:
Code: Select all | TOGGLE FULL SIZE
void
delayms (uint16_t millis)
{

So I know the void is the return type, and the millis is the parameter, but what kind of data type is a uint16_t? Is it just an integer?

And as for random numbers, that's pretty self explanatory as to what I need.

Thanks
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

by mtbf0 on Sun Jun 01, 2008 9:30 am

dude. the google is your friend. for instance for uint16_t the second result is this.

googling avr random numbers gets this as its second result. so it looks like you already have a random number generator. just be aware that unless you are able figure out a clever way to seed it, you will get the same sequence of random numbers every time you run your program. there's a reason they call these beasts pseudo random number generators.

i leave googling c function definition as an exercise, but a copy kernighan and ritchie would not be a bad thing to have.
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

by magician13134 on Sun Jun 01, 2008 9:31 am

Ah. Thanks
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

by magician13134 on Sun Jun 01, 2008 5:38 pm

How do I seed rand without using a constant? I remember in C++ we seeded random with the current time... but since these microcontrollers don't know the time after a reset... What do you use to seed it? Oh, I tried "srand(get_sys_count())", but to no avail...
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

by mtbf0 on Sun Jun 01, 2008 7:00 pm

magician13134 wrote: so I used
Code: Select all | TOGGLE FULL SIZE
int i = rand() % 9 + 1;
int j = rand() % 9 + 1;
and j always equals 6 and i always equals 5... I've looked all over the internet, but can't seem to find a reasonable solution (yes I've included stdlib.h) Any ideas?


yes. i believe i predicted this result. a computer being a deterministic thing is going to return the same sequence of random numbers every time you start it up. so you have to figure out a way to seed the random number generator. pull a number out of thin air. something. big computers use the time. so you could start a loop. wait a while after you turn on your project, then push a button and grab a count out of the loop to seed the random number generator. or something.

assuming you have a PORTB, put a pullup on pin 1 and attach an n/o switch between pin1 and ground and this might work.

Code: Select all | TOGGLE FULL SIZE
uint32_t foo;
for (foo = 0; PORTB & ~(1<< PB1); foo += 13);
srandom (foo);


did you put the define for rand max before or after you included stdlib. 'cuz it's defined in stblib.
Last edited by mtbf0 on Sun Jun 01, 2008 7:08 pm, edited 1 time in total.
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

by mtbf0 on Sun Jun 01, 2008 7:05 pm

spooky.

you deleted your post while i was quoting it.

or my laptop's imagining things.
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

by magician13134 on Sun Jun 01, 2008 7:12 pm

Ah, yes, I reread your post, and recalled seeding... But I think I figured it out, I'm writing a random number to EEPROM, then seeding with it, then writing a new random number for the next time. It seems to be working. Thanks for your help :D


Ahh! I'm finished with the breadboard version! I saw a neat video about a research of whether time "slowed down" in a crisis situation. They used a neat device called a perceptual chronometer, so I built a smaller version. A video of mine can be seen here, and there's a link to the original right below the video
http://www.youtube.com/watch?v=xqm1yBLHDWA

This device basically flashes a random number too quickly to read in a normal situation, and pressing the button displays the number to verify. Now I get to go to King's Island to test :D
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

by darus67 on Mon Jun 02, 2008 3:24 pm

I was going to suggest saving a seed in EEPROM, but you beat me to it.

Another possibility might be to read data from a digital input pin that is left
floating.
"He's just this guy. You know?"
darus67
 
Posts: 246
Joined: Wed Sep 26, 2007 10:25 pm
Location: Minnesota, USA

by magician13134 on Mon Jun 02, 2008 4:57 pm

Only problem is I don't have anymore IO pins! I've never used this many before, it's kind of exciting!
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

by mtbf0 on Tue Jul 01, 2008 11:07 pm

the new issue of servo has an article about random number generators, including reading random values off of an unconnected adc pin.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

by franklin97355 on Wed Jul 02, 2008 12:56 am

millis is the count of ms since the device was reset. It just might be random enough to be a seed.

franklin97355
 
Posts: 21215
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Please be positive and constructive with your questions and comments.