Thread: random numbers

  1. #1
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49

    Question random numbers

    in c++ how does one create a code to generate a random number that is less than x?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    use the standard function rand()?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Use srand() to seed, and rand() to actually generate the number. Use the modulus operation on the output of rand() to control the maximum value you will receive.

    Code:
    int x = rand() % 10;
    In this example, the number will be between 0 and 9.

  4. #4
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49
    thanks a lot!

  5. #5
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49
    k it works and i have written the code
    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<stdlib.h>
    main()
    {
          int a=rand()%10;
          
          cout<<a;
          
          getch();
          return 0;
          }
    i did some research and found out the necessary header files..but i have a problem

    the problem goes like this
    1->i compile the file
    2->i run the file and say i get 10 as an output
    3-> i close the window and again run the file
    4->now i get the same output 10...

    could this output be changed ?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As suggested, use srand() with a suitable seed - that "initiates the random sequence".

    Try looking at www.eternallybefuzzled.com for some examples of random number generation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by matsp View Post
    As suggested, use srand() with a suitable seed - that "initiates the random sequence".

    Try looking at www.eternallybefuzzled.com for some examples of random number generation.

    --
    Mats
    Befuzzled?

    www.eternallyconfuzzled.com
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hk_mp5kpdw View Post
    Doh :-(

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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

Tags for this Thread