Noob question for random number

This is a discussion on Noob question for random number within the C++ Programming forums, part of the General Programming Boards category; Ok, I'm new to c++, I'm trying to make my second program. And I would like to know what is ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    8

    Noob question for random number

    Ok, I'm new to c++, I'm trying to make my second program. And I would like to know what is the c++ operation to get a random number. Can you help me??

    And as we are there, can you give it to me in a usefull syntax??

    Like can you tell me how to get:

    x = ( random number between 1 and 10 )

  2. #2
    alkis_y3k
    Guest
    This might not be the precise syntax, but:
    Code:
    int h = rand() % 100;
    This will generate a 'fake' random number from 1 to 100 i think. However, I could be mistaken. Anyone?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    > x = ( random number between 1 and 10 )
    int x = (int)( (double)rand() * 10 / RAND_MAX + 1 );

    >This will generate a 'fake' random number from 1 to 100 i think.
    From 0 to 99.

    >int h = rand() % 100;
    This is a bad way to get a random number for reasons which I've stated many times before, do a board search for more details.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by alkis_y3k
    This might not be the precise syntax, but:
    Code:
    int h = rand() % 100;
    This will generate a 'fake' random number from 1 to 100 i think. However, I could be mistaken. Anyone?
    You're a little off, there. The code u gave will generate a random number from 0 to 99.

    SKINp, do a search of this board for random numbers. You should be able to find tons of threads on generating random numbers within certain bounds and using your computer's internal clock to make the pseudo-random numbers seem more random.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    If you would like true random numbers and have alot of money, I bet the people at LavaRand would be glad to make you a setup.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    8
    Hee in each of your way, I get an error telling me:

    implicit declaration of fonction 'int rand(...)'

  7. #7
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Make sure to include stdlib.h.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. printing number from array question..
    By transgalactic2 in forum C Programming
    Replies: 41
    Last Post: 12-25-2008, 04:04 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  4. Real Noob question
    By Dark Greek in forum C++ Programming
    Replies: 8
    Last Post: 09-09-2007, 07:49 PM
  5. pin number question
    By melee in forum C Programming
    Replies: 19
    Last Post: 12-11-2004, 01:39 PM

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