Thread: Generating one of three different numbers randomly

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

    Generating one of three different numbers randomly

    Hi. I read the faq on random number generation, but I would like to be able to generate one of three different numbers (only) in a random sequence.

    In other words, I have three "levels": 15, 25, 35. I want to be able to randomly generate one of these three values (not a value in between these numbers; I just want strictly 15, 25, or 35 to be generated in a random fashion).

    Is it possible to do this with srand and rand?

    Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    int array[ ] = { 15, 25, 35 };
    answer = array[ rand() % 3 ];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8

    Smile Much obliged!

    Thanks for helping me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. randomly generate numbers
    By maybabier in forum C Programming
    Replies: 10
    Last Post: 05-06-2009, 01:13 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Help generating random numbers in MFC
    By drb2k2 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:52 AM
  4. unique randomly generated numbers
    By grobe59 in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2001, 08:26 PM
  5. arranging randomly generated numbers in ascending order
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2001, 07:14 PM