Generating one of three different numbers randomly

This is a discussion on Generating one of three different numbers randomly within the C Programming forums, part of the General Programming Boards category; Hi. I read the faq on random number generation, but I would like to be able to generate one of ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 07:26 PM
  5. arranging randomly generated numbers in ascending order
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2001, 06:14 PM

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