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

    Print a random number from set

    For each of the following sets of integers, write a single
    statement that will print a number at random from the set.

    c) 6,10,14,18,22

    I can't do this in one line. the best i can do is:

    num = 1;
    while((num % 4) || (num > 24)) num = rand() % 24 + 8;
    num -= 2;
    cout << "Number in set {6,10,14,18,22}: " << num << endl;
    return 0;

    Any thoughts?

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    I'm not checking your actual code at all, but it seems like the
    question's only asking you to print out one number so there
    doesn't appear to be a need for a loop. You can probably just
    do something like:
    Code:
    cout << rand() * whatever << endl;
    --Paul

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    Well, since this looks like a homework problem,
    I'll just give a hint :

    You want to print out one of five numbers. rand()%5
    will generate one of five numbers (0,1,2,3,4)

    So (the simplest) thing to do is to come up with an
    expression such that:

    when rand() returns 0, the expression evaluates to 6,
    when rand() returns 1, the expression evaluates to 10,
    when rand() returns 2, the expression evaluates to 14,

    etc.

    As Paul mentioned, there is no need for a loop.

    Also, I am not sure if rand() returns a non-negative
    number on all systems, so maybe you might need to use

    static_cast<unsigned int>( rand() )
    Last edited by Philip Nicoletti; January 23rd, 2003 at 12:33 AM.

  4. #4
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    int numbers[] = { 6,10,14,18,22 };

    see what you can do

  5. #5
    Join Date
    Mar 2002
    Location
    AhuhA
    Posts
    204

    Exclamation (^_^)

    I think you can also do like this (hint): enter stepnumber to jump(in your ex, it is 4), then input lowerlimit(6) and upperlimit(22), then use random funtion, and finally output the number after randomly chosen.

    Hope this helps.



    Regardlessly,

  6. #6
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    HHughes, this is a very simple program so it doesn't really matter here, but to generate random integers from 0 to a certain maximum, the follwoing doesn't produce very good random results:

    x = rand() % (max+1)

    Instead the following statement produces better results:

    x = ((unsigned long)((rand()/(RAND_MAX+1.0))*(max+1.0)))

    The long in the cast above could be replaced by any unsigned integer type.

    Anyway, you'd have to perform an analysis on many random numbers before you really noticed the difference, but I thought I'd let you know in case someday you come across a project where generating sequences of random numbers was important (of coarse you probably wouldn't want to use rand() then anyway -- oh well).

  7. #7
    Join Date
    Aug 2002
    Location
    United States
    Posts
    729
    i suggest Philip Nicoletti's suggestion.

    i've never seen rand() generate a negative number. it's an int so i guess it could support it but i am next to certain that it doesn't unless you were to change it around somehow. modulus division operator is the same.

  8. #8
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673
    Hi Kevin,

    I'm no random number generator expert but I saw someone posted this statement in GameDev forum.

    x=max*rand()/(RAND_MAX+1);//generate random numbers from 0 to max-1

    And according to that poster, this will evaluate to this(below), in an optimising compiler,

    x=( max*rand() )>>15;

    Which he claimed is faster than using modulus. And note he did not have any int or float conversions.

    Although I have never tried it out myself, I feel that what he said, made sense to me.

    For those who might not be familiar, RAND_MAX is 32767(0x7fff) in VC CRT library.
    Last edited by CBasicNet; January 23rd, 2003 at 06:50 AM.

  9. #9
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673
    In case anyone wonder why num/32768 EQUALS num>>15,

    num/2 == num>>1
    num/4 == num>>2
    num/8 == num>>3
    num/16 == num>>4
    num/32 == num>>5
    .
    .
    .
    .
    num/16384 == num>>14
    num/32768 == num>>15

  10. #10
    Join Date
    Aug 2001
    Location
    Israel
    Posts
    101
    very simple:

    you have 5 numbers, with an indentical difference of 4 between them.

    so you need the following expression:
    cout << 6 + (rand() % 5) * 4;

  11. #11
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    I never noticed that I would have done;
    int numbers[] = { 6,10,14,18,22 };
    cout << numbers[ int(rand() % 5) ];

  12. #12
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Hi Kevin,

    I'm no random number generator expert but I saw someone posted this statement in GameDev forum.

    x=max*rand()/(RAND_MAX+1);//generate random numbers from 0 to max-1

    And according to that poster, this will evaluate to this(below), in an optimising compiler,

    x=( max*rand() )>>15;

    Which he claimed is faster than using modulus. And note he did not have any int or float conversions.

    Although I have never tried it out myself, I feel that what he said, made sense to me.

    For those who might not be familiar, RAND_MAX is 32767(0x7fff) in VC CRT library.
    CBasicNet,

    You bring up a good point however:

    You are only right assuming:
    * RAND_MAX is 32767 (this is not specified by ANSI)
    Incorrect: * max <= RAND_MAX/2 (Thanks CBasicNet)
    * and the size of int is 32 bits (again not specified by ANSI)

    So for VC6, this would work, but for LabWindows CVI or old 16-bit compilers (or new compilers where RAND_MAX was 2147483647) this would not work. The method I pointed out will be portable across different compilers.

    For anyone who is interested, an excellent reference on pseudo-random number generation should check out Numerical recipes' chapter 7 (especially section 7.1): http://www.ulib.org/webRoot/Books/Nu.../bookcpdf.html
    Last edited by KevinHall; January 23rd, 2003 at 12:21 PM.

  13. #13
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673
    Originally posted by KevinHall
    You are only right assuming:
    * RAND_MAX is 32767 (this is not specified by ANSI)
    * max <= RAND_MAX/2
    * and the size of int is 32 bits (again not specified by ANSI)
    if RAND_MAX is 32767 , why max should be <= RAND_MAX/2 when RAND_MAX * RAND_MAX is still < 2147483647 ?

  14. #14
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Originally posted by mwilliamson
    I never noticed that I would have done;
    int numbers[] = { 6,10,14,18,22 };
    cout << numbers[ int(rand() % 5) ];
    That's two lines, though.

  15. #15
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    CBasicNet,

    You're right -- brain fart there. For some reason I was thinking 32767 was 2^16 - 1 not 2^15 -1.

    - Kevin

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