Thread: rand()%100 not working

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    rand()%100 not working

    Okey, so i got this program from this site itself.....here is the link http://www.cprogramming.com/complete/guessans.html

    Okey, so whenever i run this, the number to be guessed is always 46....why isnt it changing??
    Last edited by striker; 12-12-2006 at 05:45 AM.

  2. #2

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by striker
    Okey, so i got this program from this site itself.....here is the link http://www.cprogramming.com/complete/guessans.html

    Okey, so whenever i run this, the number to be guessed is always 46....why isnt it changing??
    the random number will always be the same at the start, if you put the programme through a loop it will change but then you will have to guess a different random number each time, there is a way around it, if you get rand to give you a random number then run a loop around that value going back to rand every time you guess the number correctly for a new value, hope this makes sence, i'm still a noob too
    WhAtHA hell Is GoInG ON

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    The FAQ that joni linked to, shows you how to seed the random number generator using srand(). When you seed with the time, you get an unpredictable result, which is what you want.

    the random number will always be the same at the start, if you put the programme through a loop it will change but then you will have to guess a different random number each time...
    Actually, if you forget to seed, or if you use the same seed number, you will get a different number each time through the loop, but you'll get the same sequence of numbers every time you run the program. If it's predictable and repeatable, it's not random!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM