Thread: reandom numbers

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    43

    random numbers

    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
    int a;
    a=random;
    cout<<a<<endl;
    return 0;
    }
    i want to learn how to do random numbers i know that is completly wrong but how would i make a=a random number?
    Last edited by makveli; 11-11-2003 at 10:08 AM.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    try searching the board (http://cboard.cprogramming.com/showt...threadid=47035), but i'll write some random int generating code...
    Code:
    #include <iostream>
    #include <conio.h>
    #include <cstdlib>
    #include <ctime>
    
    int main()
    {
         srand(clock());   //to make the random number random
         int randInt=rand();   //a large random int
    
         std::cout<<randInt<<std::endl
               <<"Press any key to continue . . . ";
         getch();
    
         return 0;
    }
    Last edited by major_small; 11-11-2003 at 10:54 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    43
    that code dosent compile

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    that helps how about some errors?

    edit: the 'c' is lowercase in clock()
    Last edited by major_small; 11-11-2003 at 10:47 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    43
    srand(Clock()); there is a problem there it sais "warning in function int"
    'clock undeclared'
    (each underclared

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    check my last post
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    43
    u dont have a compiler in front of u wat about behind you?

  8. #8
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Originally posted by makveli
    u dont have a compiler in front of u wat about behind you?
    Don't be such a douche-bag when someone is trying to help you.

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    43
    lol i aint tryin to be offensive but his last post sais somethin like "dont expect us to do all the work for you, not all of us have a compiler in front of us"
    and he was just tryin to get help

  10. #10
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    You greatly misinterpretted that rule...

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Note: If you don't feel like including <ctime> and you have <windows.h> included, i.e. you're writing a non-console Windows app, GetTickCount() should suffice instead of clock().
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Registered User
    Join Date
    Nov 2003
    Posts
    43
    thanx but could anyone write a simple program using random numbers and show us please

  13. #13
    Registered User Red Haze's Avatar
    Join Date
    Nov 2003
    Posts
    30

    Here's what I use

    Here's what I use to do random numbers:

    ---***---

    include <iostream.h>
    include <stdlib.h>
    include <time.h>

    Code:
    int main ()
    {
        int num;
        double flt;
    
        // seed the random number generator
        srand (unsigned (time (NULL) ) );
    
        // a random integer from 0 to 10
        int = rand() % 11;
        cout << "integer, 0 to 10 --> " << int << endl;
    
        // a random float from 0 to 10
        flt = rand() / double (RAND_MAX) * 10;
        cout << "float, 0 to 10 --> " << flt << endl;
    
    
        return 0;
    }
    ---***---

    The function rand() simply returns an integer from 0 to a constant RAND_MAX.


    ---[Red Haze]---
    ----[Red Haze]----

  14. #14
    Registered User
    Join Date
    Nov 2003
    Posts
    43

    Unhappy

    its has alot of parse errors it dont work

  15. #15
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    the program i gave you is pretty much as simple as you can get, but here's something even easier (but much less random):
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main()
    {
         srand(rand());   //to make the random number random... with a random number
         int randInt=rand();   //a large random int
    
         cout<<randInt<<endl
               <<"Press any key to continue . . . ";
         cin.get;
    
         return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM