stuck!

This is a discussion on stuck! within the C++ Programming forums, part of the General Programming Boards category; I have tried all of the other posts on this forum, and none of them seem to work! All I ...

  1. #1
    Unregistered
    Guest

    Angry stuck!

    I have tried all of the other posts on this forum, and none of them seem to work! All I want is a completely random number, one that will be different each time i run the program! is this possible in c++?

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825

  3. #3
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    Dude, take five minutes to look in a book and it will tell you, its very simple.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You won't get a completely random number without a geiger counter. The best you can do with straight programming would be pseudorandom numbers, such as those created by the rand function in stdlib.h

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

  5. #5
    Unregistered
    Guest
    Originally posted by Prelude
    You won't get a completely random number without a geiger counter.
    You mean I need some Uranium or something??

  6. #6
    His posts are far and few Esparno's Avatar
    Join Date
    Mar 2002
    Posts
    100
    Uranium wont work, not enough randomness, try Plutonium instead :P.
    Signature is optional, I didnt opt for one.

  7. #7
    Unregistered
    Guest

    Unhappy

    I tried the one in tha faq! it just gives me the same number, over and over and over...

  8. #8
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    Code:
    #include <stdlib.h> //for rand
    #include <stdio.h> //for printf
    #include <time.h>   //for time
    
    int main(void)
    {
        srand(time(NULL));
        printf("A random number from 0 to 99: %d", rand() % 100);
        return 0;
    }
    Taken directly from the FAQ. This should produce another random number each time you run it.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  9. #9
    Unregistered
    Guest
    Is there anyway to make it into an integer number, like
    int x=...and so on

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    #include <stdlib.h> //for rand
    #include <iostream.h> //for I/O stuff
    #include <time.h>   //for time
    
    int main(void)
    {
        srand(time(NULL));
        int x = rand() % 100;
        cout << "x is a random number from 0 to 99"  << x << endl;
        return 0;
    }
    C++ version.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 11:40 AM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM

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