randomizing a variable

This is a discussion on randomizing a variable within the C++ Programming forums, part of the General Programming Boards category; is this simply done by: int varible=random(x) is this right? If not could someone help me?...

  1. #1
    Registered User Octorok101's Avatar
    Join Date
    May 2002
    Posts
    22

    randomizing a variable

    is this simply done by:

    int varible=random(x)

    is this right? If not could someone help me?

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    21
    read faq.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Us this code for random integer.

    -----
    int num = 0;

    num = static_cast<int>((((static_cast<double> (rand())) / (static_cast<double> (RAND_MAX + 1))) * 256 + 0));
    -----

    In the code above, the "256" is the maximum limit you want the integer to be. The 0 is the minimum limit. For example, let say you want to generate an integer from 99 to 108. You will this this code:

    -----
    // static_cast<int>((((static_cast<double> (rand())) / (static_cast<double> (RAND_MAX + 1))) * 108 + 99));
    -----

    Kuphryn

  4. #4
    Unregistered
    Guest
    Why all the static casts in there?

    Although you might be able to use previous example I would strongly advice to use the code from the FAQ instead.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    The code I posted is very, very close to being random. In his book, Bjarne Stroustrup made a strong point of not using rand() because it is not truly random.

    Kuphryn

  6. #6
    Registered User Octorok101's Avatar
    Join Date
    May 2002
    Posts
    22
    thanks

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    7

    random

    the way most easy to use the random is


    example:

    int var1;

    var=rand()%1+100; // 1 to 100 numbers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 04:31 PM
  3. Replies: 2
    Last Post: 04-12-2004, 02:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 02:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM

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