CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 30
  1. #1
    Join Date
    Jun 2001
    Posts
    21

    how to produce a 6 bit random integer

    how to produce a 6 bit random integer. i means how to control the bits
    e.g.

    213432,
    312356,
    ,,,,,

    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>

    main()
    {
    int i;
    srand( (unsigned)time( NULL ) );

    /* Display 10 numbers. */
    for( i = 0; i < 10;i++ )
    printf( " %6d\n", rand() );

    printf("new line \n");

    }

  2. #2
    Join Date
    Sep 2002
    Posts
    1,747

    bits, digits, base, or?

    6 bits:
    rand() % 64

    but 213423 is six digts? base 6? I'm sorry, but I don't quite understand... If you post a clarification, I would love to try to help!
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  3. #3
    Join Date
    Feb 2002
    Posts
    5,757
    Do you mean a six digits random number? Otherwise, use bit shift << operator. An integer is 32-bits in Windows.

    Kuphryn

  4. #4
    Join Date
    Oct 2002
    Location
    Tx, US
    Posts
    208
    -----------------------------------------------
    how to produce a 6 bit random intege
    ----------------------------------------------------
    Assuming u need 6 digit number

    The rand functions returns a number between 0 to RAND_MAX
    By standard the RAND_MAX is implementation dependant
    for Microsoft compiler this is 0x7fff
    So therotically u can not get number bigger than 0x7fff in VC++ by random method

    kuphryn provides shifting solution which is going to give u six digit number but the last digit always going to be same

    Vinod

  5. #5
    Join Date
    Nov 2002
    Location
    San Diego, USA
    Posts
    48
    If you are looking at a more reliable random stream of data, you could try the implementations in OpenSSL or Crypto++.
    Anuj Seth

    http://www.anujseth.com/

    YahooPOPs!: A Windows/Linux POP3/SMTP-to-web gateway that gives free POP3 and SMTP access to your Yahoo Mail!

  6. #6
    Join Date
    Sep 2000
    Location
    Antwerp, Belgium
    Posts
    101
    i think for a 6 bit one galathaea's answer would be correct
    for a 6 digit one you could try something like
    1000*(rand()%1000)+(rand()%1000)

    of course initializing first with srand

  7. #7
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020

    Just to clarify s. roelants' answer

    Problem with rand() on some machines is that the range is from 0 to RAND_MAX. On a lot of compilers, this happens to be 32767. What is being done in the solution is to get two pseudo random 3 digit numbers and concatenate them.
    Succinct is verbose for terse

  8. #8
    Join Date
    Sep 2002
    Location
    Philadelphia ***Epoch: Timeless***
    Posts
    560
    When I make random numbers that way, I find that the fewer digits you take, the more "random" they are. I once made a short little function that gives you a random number of n digits. Here's the gist of it (although I'm not sure it will compile because it will probably have typos here):
    Code:
    int randomnum(int a)/*a is the number of digits you want it to have*/
    {
    int b=0;
    for(int c=0;c<a;c++)
    {
    b*=10;
    b+=rand()%10;
    }
    return b;
    }
    You could simply use this with a=6.
    SolarFlare

    Those who cling to life die and those who defy death live. -Sun Tzu

    cout << endl;
    return 0;
    }

  9. #9
    Join Date
    Sep 2002
    Posts
    1,747

    concerning random character of concats

    Solarflare is absolutely correct. Since RAND_MAX is not an exponent of ten (on any machine I've seen, but generally its at least not guaranteed to be), there will be some numbers that will have a greater likelihood of occuring than others.

    For example: take RAND_MAX = 32767
    taking % 1000 gives

    33 possibilities to get #s 0 - 767
    32 possibilities to get #s 768 - 999

    but taking % 10 gives

    3277 posibilities to get #s 0 - 7
    3276 possibilities to get #s 8, 9

    By choosing a smaller modulus to concatenate makes the likelihood ratios closer to 1:1, thus giving a better random character. Of course, the best way is to just exclude the numbers from RAND_MAX down to RAND_MAX - RAND_MAX % N (N = 10, 1000, etc) as contributing to the asymmetry by taking another rand if such occurs (repeat as necessary). Then, its a tradeoff between rand returns to exclude and number of calls of rand to concatenate...
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  10. #10
    Join Date
    Sep 2002
    Location
    Philadelphia ***Epoch: Timeless***
    Posts
    560
    If you take rand()%(2 to some power less than 16) then you will get a truly random number. Even rand()%10 has some (ignorable) flaws because it's not a power of two. But it's easier to work with .
    I suppose you could have
    Code:
    int randomnum(int a)/*a is the number of digits you want it to have*/
    {
    int b=0;
    for(int c=0;c<a;c++)
    {
    b*=10;
    b+=rand()%8;
    b+=rand()%2;
    }
    return b;
    }
    and it would be even better.
    SolarFlare

    Those who cling to life die and those who defy death live. -Sun Tzu

    cout << endl;
    return 0;
    }

  11. #11
    Join Date
    Sep 2002
    Posts
    1,747

    oops!

    I don't know how to delicately do this... but there are a couple of flaws in SolarFlare's last post...
    The rands will only give an integer from 0 to 8, not 0 to 9 (0-7 plus 0, 1) and also, the endpoints 0, 8 will have less of a chance (only one possibility each) of being picked than 1-7 (2 possible ways each). But I absolutely agree with the opinion that, for around the house type didn't get out of my pjs today random number generation, the %10 solution's side effects are ignorable.
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  12. #12
    Join Date
    Sep 2002
    Location
    Philadelphia ***Epoch: Timeless***
    Posts
    560

    Re: oops!

    Originally posted by galathaea
    I don't know how to delicately do this... but there are a couple of flaws in SolarFlare's last post...
    NOOOO!!!!! now I must commit ritual suicide!!!! I'm a failure!!!!!
    I mean, I just forgot to check over my logic
    Hmmm... I guess there's no easy way, then, to get a truly random decimal number unless you have something like this:
    Code:
    b=10;
    while(b>=10)
    {
    b=rand()%16;
    }
    but it's not very efficient.
    SolarFlare

    Those who cling to life die and those who defy death live. -Sun Tzu

    cout << endl;
    return 0;
    }

  13. #13
    Join Date
    Sep 2002
    Location
    Philadelphia ***Epoch: Timeless***
    Posts
    560
    Or, to increase the likelihood that it won't have to loop more than once:
    Code:
    do
    {
    d=rand()%512;
    b=d%10;
    }while(d>=510);
    Now it will only repeat the loop once every 256 times you run it.
    SolarFlare

    Those who cling to life die and those who defy death live. -Sun Tzu

    cout << endl;
    return 0;
    }

  14. #14
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    First off, using the line:

    rand()%(maxval + 1)

    to produce a random integer between 0 and maxval *generally* does not produce uniformly random results. As solarflare pointed out, for most implementations, if maxval is ((power of 2) - 1), then this will produce uniformly random results assuming rand() produces uniformly random results.

    I have worked in the science and engineering fields for a while and do not trust most compilers' implementations of rand(). I personally recommend reading chapter 7 of Numerical Recipes for C. This has good algorithms for random numbers and some suggestions for producing the very results you wanted.

    PDF versions of Numerical Recipes for C are available online at:
    http://www.ulib.org/webRoot/Books/Nu.../bookcpdf.html

    Hope this helps!

    - Kevin

  15. #15
    Join Date
    Jul 2002
    Location
    American Continent
    Posts
    340
    Using modular or divide operation to get random number of a specific range is extremely slow and gives you none-uniform distribution. So it is not recommended.

    Here is a function that is fast, and gives you uniform distribution and is completely random:

    Code:
    int GetRand()
    {
        static  int nPrevious = 0;
    
        if (((nPrevious&0xff) ^ (nPrevious>>12)) == 0)
        {
            // Make sure we re-seed once in a random while.
            srand(nPrevious ^ GetTickCount());
        }
        do {
            int tmp = rand();
            nPrevious = ((tmp<<5) - tmp) ^ (nPrevious>>1);
        } while (nPrevious > 999999 || nPrevious < 100000);
    
        return nPrevious;
    }

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured