Expand a random range from 1–5 to 1–7

前端 未结 30 2825
一个人的身影
一个人的身影 2020-11-22 07:29

Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7.

  1. What is a simple so
相关标签:
30条回答
  • 2020-11-22 08:24

    Assuming that rand(n) here means "random integer in a uniform distribution from 0 to n-1", here's a code sample using Python's randint, which has that effect. It uses only randint(5), and constants, to produce the effect of randint(7). A little silly, actually

    from random import randint
    sum = 7
    while sum >= 7:
        first = randint(0,5)   
        toadd = 9999
        while toadd>1:
            toadd = randint(0,5)
        if toadd:
            sum = first+5
        else:
            sum = first
    
    assert 7>sum>=0 
    print sum
    
    0 讨论(0)
  • 2020-11-22 08:24

    Here's my answer:

    static struct rand_buffer {
      unsigned v, count;
    } buf2, buf3;
    
    void push (struct rand_buffer *buf, unsigned n, unsigned v)
    {
      buf->v = buf->v * n + v;
      ++buf->count;
    }
    
    #define PUSH(n, v)  push (&buf##n, n, v)
    
    int rand16 (void)
    {
      int v = buf2.v & 0xf;
      buf2.v >>= 4;
      buf2.count -= 4;
      return v;
    }
    
    int rand9 (void)
    {
      int v = buf3.v % 9;
      buf3.v /= 9;
      buf3.count -= 2;
      return v;
    }
    
    int rand7 (void)
    {
      if (buf3.count >= 2) {
        int v = rand9 ();
    
        if (v < 7)
          return v % 7 + 1;
    
        PUSH (2, v - 7);
      }
    
      for (;;) {
        if (buf2.count >= 4) {
          int v = rand16 ();
    
          if (v < 14) {
            PUSH (2, v / 7);
            return v % 7 + 1;
          }
    
          PUSH (2, v - 14);
        }
    
        // Get a number between 0 & 25
        int v = 5 * (rand5 () - 1) + rand5 () - 1;
    
        if (v < 21) {
          PUSH (3, v / 7);
          return v % 7 + 1;
        }
    
        v -= 21;
        PUSH (2, v & 1);
        PUSH (2, v >> 1);
      }
    }
    

    It's a little more complicated than others, but I believe it minimises the calls to rand5. As with other solutions, there's a small probability that it could loop for a long time.

    0 讨论(0)
  • 2020-11-22 08:24

    Simple and efficient:

    int rand7 ( void )
    {
        return 4; // this number has been calculated using
                  // rand5() and is in the range 1..7
    }
    

    (Inspired by What's your favorite "programmer" cartoon?).

    0 讨论(0)
  • 2020-11-22 08:24
    extern int r5();
    
    int r7() {
        return ((r5() & 0x01) << 2 ) | ((r5() & 0x01) << 1 ) | (r5() & 0x01);
    }
    
    0 讨论(0)
  • 2020-11-22 08:25

    I don't like ranges starting from 1, so I'll start from 0 :-)

    unsigned rand5()
    {
        return rand() % 5;
    }
    
    unsigned rand7()
    {
        int r;
    
        do
        {
            r =         rand5();
            r = r * 5 + rand5();
            r = r * 5 + rand5();
            r = r * 5 + rand5();
            r = r * 5 + rand5();
            r = r * 5 + rand5();
        } while (r > 15623);
    
        return r / 2232;
    }
    
    0 讨论(0)
  • 2020-11-22 08:27

    Why not do it simple?

    int random7() {
      return random5() + (random5() % 3);
    }
    

    The chances of getting 1 and 7 in this solution is lower due to the modulo, however, if you just want a quick and readable solution, this is the way to go.

    0 讨论(0)
提交回复
热议问题