Random!!!

This is a discussion on Random!!! within the C++ Programming forums, part of the General Programming Boards category; I can't figure out how to do random numbers!!!! I want to make a random # between 1 and 100, ...

  1. #1
    Unregistered
    Guest

    Random!!!

    I can't figure out how to do random numbers!!!! I want to make a random # between 1 and 100, but It makes the number whatever it wants! How do I set a range, and make it so the random number is inputed into a variable?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    FAQ!!! Nobody reads it, so we will keep seeing this question.

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

  3. #3
    Unregistered
    Guest
    I read it, but I still don't understand them.

  4. #4
    DarkSFK
    Guest
    Thisis something I would like to know also, if someone could be so kind.

  5. #5
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Talking I'm just what the doctor ordered!

    This is a random number generator I made because I was not satisfied by the one provided by Standard C/C++.

    This one is automatically seeded after you declare the class object within your code and you simply have to call the random_tololot() function.

    You can just hotwire the source like so:

    PHP Code:
    #include "random.cpp"

    int main(void)
    {
      
    RANDOM go;
      
    int num;
      
      
    /*...some other code...*/
      
    num go.random_tololot(11000);
      
    /*...some more code...*/

      
    return 0;

    I'm assume you know how to handle simple classes. If you do, then just fire away!
    Attached Files Attached Files
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    22
    Hi,

    Another option --

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
            int i;
            int minumum = 1;
            int maximum = 100;
    
            srand(time(NULL)); /* seed randomizer with current time makes rand() rather more unique on each call*/
            i = minumum + (int)(rand()%maximum); /* simple but not recommended */
            /* i = minumum + (int)((double)maximum*rand()(RAND_MAX+1.0)); is better */
            printf("\ni is %d\n", i);
    }
    -Gerald
    -------------------------
    Gerald.

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    16
    beg_g, I believe you must take the modulus as (max-min+1) to get the number in range. Otherwise you could have numbers outside the range.

    Ex.

    min = 98; max = 100; rand is 44
    ->rand % max = 44
    ->min + (rand % max) = 142

    or

    rand % (max-min+1) = 2
    min + rand %(max-min+1) = 100

    --Chorus

    PS: depending on the random number generator, I believe you can lose "randomness" by taking modulus. You might be better off scaling the value into the appropriate range and truncating the mantissa.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 09:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 09:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 03:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 05:11 PM

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