random number generation

20 posts / 0 new
Last post
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

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

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 ), 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

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

this is a good fast integer generator
http://en.wikipedia.org/wiki/Linear_congruential_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

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

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

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

It would be nice to post in English language.

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

/Martin.

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

oh.. all people not speak norwegian!

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

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

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

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

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):

  repeat 
    random=random+1;
    if random==8 then random=0;
  until key_pressed;
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That's danish, not norwegian.

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

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

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

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
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

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".

Pages