Thread: random function

  1. #1
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269

    Question random function

    How do you make a random function work?

    other questions:
    where do you type the variable that you assign the rndm number to?
    where do you type the highest number the rndm thing will go to?
    umm........
    jus plain how do you do it?
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    Code:
    int x = rand () % 10;   // max is 9
    any ?uestions?
    hasafraggin shizigishin oppashigger...

  3. #3
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    how do you make a maximum of....well......10?
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  4. #4
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    change the '10' to '11'...
    hasafraggin shizigishin oppashigger...

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    To DoubleAnti

    > doubleanti
    > title: grand wierdo

    I believe it's spelled weirdo
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Random numbers have been covered a lot in the past couple of weeks. Read some more threads. LOOK UP.Between the buttons marked faq (probably some sort of self-destruct as no-one seems to see this) and home is another that says search on it. Do you think thats there just to look pretty like the faq button?? dont u guys do any research of your own.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    heh... it's all part of the wierdosity man... all part of it...
    hasafraggin shizigishin oppashigger...

  8. #8
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    Simple bluehead...go to my message board I'll tell you how the modulus operator works with rand()

  9. #9
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    To...

    To put it simple. Ok, you want a random number. Go like this:

    Example: x=rand() % 10;

    That means anywhere from 0 to 10, INCLUDING 10. If you want to set another boundry, go like this:

    Example: x=5 + rand() % 10;

    That means anywhere from 5 to 10, inclusive. Meaning 5 and 10 are included. Think of it like this 5>=rand()>=10. Those are greater-than-or-equal-to signs.

    If you want it to generate random numbers in a different pattern each time, you must enter this before you do the rand() thing:

    srand(time (NULL) );

    That will make it a different pattern of random numbers according to the time clock. Or if you wanted you could go like this:

    unsigned random;
    cout<<"Enter number: ";
    cin>>random;
    srand(random);
    x=5 + rand() % 10;

    Ok that will change the patter according to what the user enters. Make sure if you do it the time clock way, include "time.h". And for srand(); you need "windows.h". Well, that just about covers it.
    Last edited by Leeman_s; 11-10-2001 at 12:40 AM.

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Example: x=rand() % 10;

    That means anywhere from 0 to 10, INCLUDING 10.
    Wrong. a % b never equals b:
    Code:
    #include <cstdio>
    
    int main()
    {
        for(int i = 0; i < 20; i++)
            printf("%2d % 10 = %d\n", i, i % 10);
    
        return 0;
    }
    - lmov

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Yikes, lots of errors

    Example: x=rand() % 10;
    That means anywhere from 0 to 10, INCLUDING 10.


    Nope! It is from 0 to 9 (including 9). See this link for an earlier discussion on this:
    http://www.cprogramming.com/cboard/s...&threadid=3867

    If you want to set another boundry, go like this:
    Example: x=5 + rand() % 10;

    That means anywhere from 5 to 10, inclusive. Meaning 5 and 10 are included.


    Wrong again. That means anywhere from 5 to 14 (including 14).

    Think of it like this 5>=rand()>=10. Those are greater-than-or-equal-to signs.

    I want to see that number, which is greater than 10 but smaller than 5...

    And for srand(); you need "windows.h".

    Umm, srand() is in stdlib.h. Well, it IS possible that windows.h includes stdlib.h also...


    Well, if you're still (confused) over this. Make a program that generates some random numbrs and see where the boundries are.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM