Thread: random in Dev C++ still not working

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    4

    Unhappy random in Dev C++ still not working

    Hi!

    I want to use "random" in Dev C++ so that it chooses between two digits 1 and 2. But I don't seem to know how!

    I have tried with

    srand(time(NULL))
    rand() % 2

    but it gives me the numbers 0 and 1. Any one whol could help would be really appreciated.

    Thank You very much!

    Akmal Safi

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you can get 0 and 1 as random numbers and you need 1 and 2 then have a guess what you need to do. What's the difference between 1->2 and 0->1.
    zen

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    4

    to Zen

    thank you for ur quick reply but Could u please be a little more explicit.

    I dint quiet get what to do with smaller than and bigger than symbols.

    thank you

    akmal safi

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you get 0 or 1 then add 1 to the result to obtain either 1 or 2.
    zen

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    5
    If you were wanting to get a random number between 1 and 2?!?

    then the code would be,

    srand(time(0));
    rand()%2+1;

    You would have to put that with a variable of course

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    4

    Smile thanku heavyd

    Thank you Heavyd, it worked very well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Criteria based random numbers
    By alkisclio in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2006, 12:29 PM
  3. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  4. time Delays and Random functions
    By markphaser in forum C++ Programming
    Replies: 17
    Last Post: 02-20-2006, 07:10 PM
  5. Random Problem?
    By dizz in forum C++ Programming
    Replies: 4
    Last Post: 11-20-2002, 05:00 PM