Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

rand function

In learning the rand function, I need to know how to write a SINGLE statement that will print a number at random from these sets:

a) 2, 4, 6, 8, 10

b) 3, 5, 7, 9, 11

c) 6, 10, 14, 18, 22

Providing me with the answer or clue to at least one of those sets should help me to discover the answer to the others. I am stuck at:

n = 2 + rand () % .........what is next?

(where 2 is 3 for problem b and 6 for problem c)


Comments

  • GiantGiant Member Posts: 225
    Try something like this

    [code]
    n = 2 + (( rand() % 5 ) * 2 );
    n = 3 + (( rand() % 5 ) * 2 );
    n = 6 + (( rand() % 4 ) * 4 );
    [/code]




    The Giant
    Member of the Stupid Coders
    http://www.stupidcoders.cjb.net

    "Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." --Albert Einstein

Sign In or Register to comment.