Random numbers

This is a discussion on Random numbers within the C Programming forums, part of the General Programming Boards category; How do I generate random numbers in Visual C 6.0 in Windows? I tried rand() and random() but they return ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    98

    Random numbers

    How do I generate random numbers in Visual C 6.0 in Windows? I tried rand() and random() but they return very large numbers. How would I, for example, generate a random number between 0 and 10?
    Thanks

  2. #2
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    575
    Use the modulo function %:

    Code:
    SomeVariable = srand(something) % 11; // from 0 to 11-1
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  3. #3
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    For more sophisticated random numbers, you should have a look at preludes article on using rand().

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    Thanks!

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, 09:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 06: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, 09:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21