Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
mmmlinux
PostPosted: Sep 08, 2006 - 12:33 AM
Newbie


Joined: Apr 19, 2006
Posts: 19


i need to have a random number between 0 and 7 generated. i am using an attiny2313. can any one give me a little snippet of code.
 
 View user's profile Send private message  
Reply with quote Back to top
brunk
PostPosted: Sep 08, 2006 - 06:08 AM
Newbie


Joined: Jan 15, 2002
Posts: 18


mmmlinux wrote:
i need to have a random number between 0 and 7 generated. i am using an attiny2313. can any one give me a little snippet of code.


From the C FAQ:

Quote:
13.16: How can I get random integers in a certain range?

A: The obvious way,

rand() % N /* POOR */

(which tries to return numbers from 0 to N-1) is poor, because
the low-order bits of many random number generators are
distressingly *non*-random. (See question 13.18.) A better
method is something like

(int)((double)rand() / ((double)RAND_MAX + 1) * N)

If you're worried about using floating point, you could use

rand() / (RAND_MAX / N + 1)

Both methods obviously require knowing RAND_MAX (which ANSI
#defines in <stdlib.h>), and assume that N is much less than
RAND_MAX.

(Note, by the way, that RAND_MAX is a *constant* telling you
what the fixed range of the C library rand() function is. You
cannot set RAND_MAX to some other value, and there is no way of
requesting that rand() return numbers in some other range.)

If you're starting with a random number generator which returns
floating-point values between 0 and 1, all you have to do to get
integers from 0 to N-1 is multiply the output of that generator
by N.

References: K&R2 Sec. 7.8.7 p. 168; PCS Sec. 11 p. 172.


--
brunk
 
 View user's profile Send private message  
Reply with quote Back to top
DanFrederiksen
PostPosted: Jan 24, 2010 - 01:21 AM
Newbie


Joined: Jan 17, 2010
Posts: 5


this is a good fast integer generator
http://en.wikipedia.org/wiki/Linear_con ... _generator

I use this specific line for integers from 0 to 250
rand=(rand*109+89)%251;

init rand with some value. I start with 89. I think even 0 works. requires 16bit operations. might work with %256 too for 0-255 range. I just use primes.
short sequence of 40 numbers seems to have nice even distribution
 
 View user's profile Send private message  
Reply with quote Back to top
wolfman2004
PostPosted: Jun 24, 2010 - 10:24 AM
Newbie


Joined: Apr 23, 2010
Posts: 1


Kunne også bare gøres sådan

Dim I As Word ' dim variable

Do
I = Rnd(40) 'get random number (0-39)
Print I 'print the value
Wait 1 'wait 1 second
Loop
 
 View user's profile Send private message  
Reply with quote Back to top
mtaschl
PostPosted: Jun 24, 2010 - 10:42 AM
Resident


Joined: Aug 21, 2002
Posts: 896
Location: Austria

It would be nice to post in English language.

BTW, Bing translates it as
"Could also just be such"

_________________
/Martin.
 
 View user's profile Send private message  
Reply with quote Back to top
TPE
PostPosted: Jun 24, 2010 - 12:15 PM
Hangaround


Joined: May 02, 2006
Posts: 355
Location: Norway

oh.. all people not speak norwegian!
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
DO1THL
PostPosted: Jun 24, 2010 - 12:55 PM
Posting Freak


Joined: Aug 29, 2002
Posts: 1142
Location: Muenster, Germany

If you want a non-deterministic random generator (i.e. one not based on mathematical algorithms), just do some ADC samples of an open port line and use the low bit of consecutive samples to construct your random number.

-- Thilo
 
 View user's profile Send private message  
Reply with quote Back to top
Jepael
PostPosted: Jun 24, 2010 - 01:42 PM
Raving lunatic


Joined: May 24, 2004
Posts: 6275
Location: Tampere, Finland

I also don't speak norwegian but can still understand good enough to translate "can also be made like this".

Some algorithms are better than others, but depending on what you are planning to do with the random number, we cannot know what is overkill and what is stupid way.

For normal dice games etc it is good enough to do something like this (pseudocode):
Code:

  repeat
    random=random+1;
    if random==8 then random=0;
  until key_pressed;
 
 View user's profile Send private message  
Reply with quote Back to top
Melkekartong
PostPosted: Dec 29, 2011 - 02:37 AM
Newbie


Joined: Jan 01, 2007
Posts: 1
Location: Norway

That's danish, not norwegian.
 
 View user's profile Send private message  
Reply with quote Back to top
Paulvdh
PostPosted: Jan 01, 2012 - 01:51 AM
Hangaround


Joined: Aug 03, 2001
Posts: 335


A number from 0 to 7?
How random can 3 bits be?

There has been a pretty recent post about using the lower bit(s) of the ADC as input for a random number and a few years ago a guy wrote a nice article with several different sources for random bits in AVR processors. The ADC was one of the sources, phase noise between the avr main chrystal and a 32kHz watch chrystal was another. The article (in the pojects section?) also had (has) code examples and some analysis of the randomness of those different sources. Good read and highly recomended for danisch, norwegian and other people.

_________________
Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
snigelen
PostPosted: Jan 01, 2012 - 10:09 AM
Posting Freak


Joined: Jan 08, 2009
Posts: 1722
Location: Lund, Sweden

Paulvdh wrote:
A number from 0 to 7?
How random can 3 bits be?

That's a very good question. Here's a sequence of 20 independent random numbers from 0 to 7
Code:
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
No, I'm not kidding, it really is. It's just that 3 have a 99.99% probability to be drawn and thus a very low probability for the other numbers to share.
Here's another sequence from 0 to 7 generated with a completely different method
Code:
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
In this case I have a start number choosen with equal probability and next number be the same as the previous with 99.99% probability and with 0.01% probability be a new one choosen uniformly from 0 to 7.

These two methods will give completely different sequences in the long run, with completely different statistical properties. But they're both random sequences of 3 bit random numbers.

So how random can 3 bits be? Well they can be random in very many ways. Usually one want something more than just "random".
 
 View user's profile Send private message  
Reply with quote Back to top
Paulvdh
PostPosted: Jan 10, 2012 - 04:08 PM
Hangaround


Joined: Aug 03, 2001
Posts: 335


@sniggelen:

You're complely wrong of course, it's not "3" but it's "4".
Code:

int getRandomNumber()
{
  return 4;  // Chosen by fair dice roll.
             // Guaranteed to be random.
}
Reference:
http://xkcd.com/221/

Anyway, I always did suck at statistics at school.

Quote:
Usually one want something more than just "random".
A very good example of that is the shuffle function of a cd player. I once had a cd player with a true "random" button and it was pretty annoying when it played the same song 2 or 3 times in a row.

_________________
Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Quatar
PostPosted: Feb 23, 2012 - 09:46 AM
Newbie


Joined: Feb 23, 2012
Posts: 3


There are a variety of well-studied techniques for this. Google "pseudorandom number generation" or "pseudorandom number generator".

I believe that modern Intel chips have an instruction or somesuch that actually produces true random numbers (it uses two interfering oscillators plugged into a voltage meter, or something) but I do not know whether any programming libraries actually tap into this.


Last edited by Quatar on Jan 10, 2013 - 06:35 AM; edited 2 times in total
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Feb 23, 2012 - 10:42 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 71075
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

I believe that modern Intel chips have an instruction or somesuch that actually produces true random numbers (it uses two interfering oscillators plugged into a voltage meter, or something) but I do not know whether any programming libraries actually tap into this.

What relevance would that have to an avr-gcc programmer anyway?

In the meantime the Tutorial forum here has at least two good articles about random numbers on AVRs.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
Torby
PostPosted: Feb 23, 2012 - 02:48 PM
Raving lunatic


Joined: Nov 11, 2003
Posts: 6084
Location: Tulsa OK USA

One I tried in my younger days was to read the program's code byte by byte. It was terrible.

_________________
Torby

Cartesian coordinates are SO 17th century.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
clawson
PostPosted: Feb 23, 2012 - 02:50 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 71075
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

It was terrible.

Not only not random but presumably totally deterministic. At least if you have a number of variables in SRAM I suppose there'd be a chance if you pointed to the area (maybe the SFRs too?) then hashed/CRCd the whole lot you mioght get something a bit random?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
brisrocket
PostPosted: Feb 14, 2013 - 04:00 AM
Rookie


Joined: Mar 23, 2012
Posts: 20
Location: Brisbane

Thanks worked a treat. I know it's not purely random, but it's varied enough to suit my needs.

_________________
Australian Entrepreneur, using the AVR technology for prototyping and market testing.

http://brisrocket.wordpress.com
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Torby
PostPosted: Feb 14, 2013 - 02:49 PM
Raving lunatic


Joined: Nov 11, 2003
Posts: 6084
Location: Tulsa OK USA

TPE wrote:
oh.. all people not speak norwegian!


What a surprise.

_________________
Torby

Cartesian coordinates are SO 17th century.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
marcolinuxBR
PostPosted: Mar 05, 2014 - 03:47 AM
Newbie


Joined: Mar 05, 2014
Posts: 1


Hi people!
This is what I use. May have some bugs but worked for me (atTiny2313).

Code:

void initRand(void)
{
        uint8_t state;
        static uint8_t EEMEM sstate;

        state = eeprom_read_byte(&sstate);

        // Check if it's unwritten EEPROM (first time). Use something funny
        // in that case.
        if (state == 0xff)
                state = 0xAA;
        srand(state);
        eeprom_write_byte(&sstate, rand());
}

int main(void){

initRand();

#define SLEEPTIME 5
#define MAX_DICE 5
while(1){

//some blinking, as if rolling
for(uint8_t i=0;i<=20;i++){
    showSequence(SLEEPTIME);
    sleep(SLEEPTIME);
}


//show result
uint8_t dice = rand() / (RAND_MAX / MAX_DICE + 1);
while(1){
  showDice(dice+1);
}
}


Hope it is ok to bump an old thread, but this show up on google.
The code is from my lame dice game, to test charlieplexing. Shameless plug:
https://raw.github.com/marcolinuxbr/att ... rliedice.c

EDIT: code tags. Thanks for the hint js!


Last edited by marcolinuxBR on Mar 20, 2014 - 04:13 AM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
js
PostPosted: Mar 05, 2014 - 06:55 AM
10k+ Postman


Joined: Mar 28, 2001
Posts: 22542
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

Shameless hint here http://www.avrfreaks.net/index.php?name ... p;t=131748 Wink

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits