Chris Cai's Tech Blog

博客园 首页 新随笔 联系 订阅 管理

The world is an unpredictable place. Even more so than Mr Gump's box of choclates. It would be terribly boring of there was only 1 shape of tree in the world, and every one looked the same. Likewise, it gets boring when you're playing games and the computer players, aliens, whatever, do the same things every time you play it. This is why it is useful to have a random number generator in your programs.

 

However there is something to note here. There is no such thing as random, (except maybe on the quantum level). Generating random numbers, therefore, has alwaws been a bit of a problem. The random numbers generated by the random number function in your programming language (if it has such a thing) are not really random at all. They are infact pseudo-random. They are generated using a very complicated formula that takes a number, and returns another. The returned number should, hopefully, be very difficult for a human to predict. Beacuse it's a function, it will always return the same number when passed the same number. You usually don't see all this number passing going on, because the function passes its last generated number to itself to generate a new number when it's asked for. Anyway, all this is irrelevant really. what i'm saying is that random number generators are often quite slow. What I shall present here is a random number generator that uses just ADDs, and ANDs and a few IFs. It is really quite fast. I have a feeling that it stablises after a few hundred thousand iterations, but thats not really a problem. It'll give you quite a few random numbers before you need to reset it.

 

You can try fiddling around with the numbers and stuff, to see if you can get it any better. Its just a quick one I knocked up in a couple of mins, so I haven't really tested it very extensively. I had it incrementing the colour of random pixels on the screen and it seemed to give a very even distridution on numbers across its range of 0 to 255. Don't ask me why/how it works, I have no idea, its just chaotic or something.

 

here it is written in BASIC:

    FUNCTION randomnum () STATIC
      IF m = 0 THEN
        a = 1
        m = 32000
      END IF
      m = m - 1

      q1 = q1 + 1
      IF q1 >= v1 THEN

        q1 = 0
        p = (p + a) AND 255
        v1 = -v1
      END IF

      q2 = q2 + 1
      IF q2 >= v2 THEN

        q2 = 0
        p = (p + a) AND 255
        v2 = -v2
      END IF

      q3 = q3 + 1
      IF q3 >= v3 THEN

        q3 = 0
        p = (p + a) AND 255
        v3 = -v3
      END IF

      q4 = (q4 + 1) AND 255
      IF q4 = p THEN

        a = (a + 1) AND 15
      END IF

      v1 = p
      p = (p + v1 + v2 + v3) AND 255
      v3 = v2: v2 = v1
      return_value = v1

    END FUNCTION

and for those people who want to generate numbers at speed, here it is again in 086 assembler. And not a MUL or DIV in sight.

    m WORD 0
    p BYTE 0
    a BYTE 0
    v1 BYTE 0
    v2 BYTE 0
    v3 BYTE 0
    q1 BYTE 0
    q2 BYTE 0
    q3 BYTE 0
    q4 BYTE 0
      cmp m, 0
      jnz l1
      mov a, 1
      mov m, 32000
    l1:
      dec m
      mov dl, a
      inc q1
      mov al, q1
      cmp al, v1
      jb l2
      mov q1, 0
      add p, dl
      neg v1
    l2:
      inc q2
      mov al, q2
      cmp al, v2
      jb l3
      mov q2, 0
      add p, dl
      neg v2
    l3:
      inc q3
      mov al, q3
      cmp al, v3
      jb l4
      mov q3, 0
      add p, dl
      neg v3
    l4:
      inc q4
      mov al, p
      cmp al, q4
      jne l5
      inc a
      and a, 15
    l5:
      mov al, p
      mov v1, al
      add al, al
      add al, v2
      add al, v3
      mov p, al
      mov al, v2
      mov v3, al
      mov al, v1
      mov v2, al

and AL now contains a random number between 0 and 255

Of course, this system suffers from the same problem that it will produce the same set of random numbers every time you start it. A good way to overcome this, is to set the values of v1, v2 and v3, based on the numbers of hours mins and seconds in the computer's timer when the program starts. This way, the generator will always start with different values, and so will produce different sets of random numbers.

posted on 2009-11-14 12:06  Chris Cai  阅读(323)  评论(0编辑  收藏  举报