Thread: Random numbers

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    27

    Question Random numbers

    I got this program which I have pasted below is not behavouring as I would expect it to.

    It works fine but the thing is that it is not displaying the random numbers in the range i want it to, which is 1000-1112.

    When I run it I find that the numbers are outside of this range!
    I have no idea why?????????

    Could someone help me out
    Many thanks



    #include <iostream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    #include <time.h>

    int main()
    {
    srand( time(0));

    for ( int i = 1; i <= 20; i++ ) {
    cout << setw( 10 ) << ( 1000 + rand() % 1112 );

    if ( i % 5 == 0 )
    cout << endl;
    }

    return 0;
    }

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > ( 1000 + rand() % 1112 );

    Because you're adding 1000 to a random number between 0 and 1111.

    For 1000-1112
    ( 1000 + rand() % 113 );

  3. #3
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    i always to random numbers like this:

    [CODE]
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>

    const int HIGH = 10;
    const int LOW = 1;

    int main()
    {
    int num;
    time_t seconds;
    time(&seconds);
    srand((unsigned int)seconds);

    num = rand() % (HIGH - LOW + 1) + LOW; //that makes the random number between HIGH and LOW

    cout<<num<<endl;

    return 0;
    }
    Paro

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    27
    How would I get a random number from the following set of numbers... 2, 4, 6, 8, 10.

    I thought of do... (2 + rand() % 8);
    but that doesn't work.

    Many thanks in advance

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    2 * (1 + rand() % 4)

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    int table[]={2,4,6,8,10}
    int i = rand()%5;
    int myRandomNumber=table[i];

    thats one way there are many more.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    27
    Could you please explain this to me.

    From what I understand, the above code is saying that the range is from 0 to 4, but than multiply by 2.

    Are you sure it is multiply, shouldn't it be plus?

    I don't kmow, can u please explain

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    rand()%4 give a random number in range 0-3
    1+rand()%4 therfore gives a number in range 1-4
    now 2*1=2
    2*2=4
    2*3=6
    2*4=8

    unfortuneately it doesn't make 10 lol but you can figure out how to now i'm sure.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Well, first of all, it should be 5, not 4 (I don't know what I was thinking).

    rand() % 5 gives a random numbe between 0 and 4
    Adding 1 puts it between 1 and 5
    Multiplying by 2 gives eitehr 2, 4, 6, 8, or 10

  10. #10
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Concurrency again...

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    27
    Huhhhhhhhh I see now

    Many thanks

    I am just not thinking right LOL

  12. #12
    Registered User
    Join Date
    Feb 2002
    Posts
    27
    what if i want random numbers from a set of, 3,5,7,9,11

    I thought about 2*(3+rand()%8) but that's wrong
    than i thought.....
    2*(3+rand()%8)-1 but that's wrong too


    pleaseee help


    many thanks in advance

  13. #13
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Think about it - that set's only one above the one from before, so just do

    (2 * (1 + rand() % 5)) + 1

  14. #14
    Registered User skillet's Avatar
    Join Date
    Feb 2002
    Posts
    20
    or easier:

    3 + 2 * rand()%5

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM