Random Numbers

This is a discussion on Random Numbers within the C++ Programming forums, part of the General Programming Boards category; How do I get random values, eg. between 1 and 10 ? Also, can I write : Code: if (name=="me") ...

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Random Numbers

    How do I get random values, eg. between 1 and 10 ?

    Also, can I write :

    Code:
    if (name=="me")
    {
    int age == age + 2
    }
    or something of the sort ? I have an integer which already has a value, and I want to add a modifier if certain things occur, in this case that name is "me".

    Thanks !

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Code:
    //it would be like this
    
    int age;
    if(name == "me") {
        age += 2;
    }

  3. #3
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Code:
    int age;
    if(name=="me")
    {
    age += 2;
    }
    
    // That would work ? Can I also do
    
    {
    age -= 2;
    }
    
    // ? Thanks...
    And can you tell me how to get random numbers ? Thanks a lot for your help.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    yes you can do:
    age -= 2;

    for random numbers:

    http://www.cprogramming.com/boardfaq.html#random

    there are also a number of threads covering this topic, do a search, you'll be able to find the answer to your question.

  5. #5
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    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;
    
    }
    What is srand(time(NULL)); ?
    What's %d in the printf section ?




    Also,
    Code:
    int rand_mid(int low, int high)
    
    {
    
       return low+rand()%(high-low+1);
    
    }
    If I used this, I'd write something like :

    cout << rand_min(1, 10)

    to get random numbers between 1 and 10 ?
    Last edited by Korhedron; 12-23-2002 at 01:44 PM.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    sure...haven't tried, but it should work. assuming it is in a member function in the struct.

    edit:: I'd like to know if it works.

  7. #7
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Sorry, I was modifying my last post when you posted your reply... I'll repeat it :




    Code:
    int rand_mid(int low, int high)
    
    {
    
       return low+rand()%(high-low+1);
    
    }

    If I used this, I'd write something like :

    cout << rand_min(1, 10)

    to get random numbers between 1 and 10 ?
    Last edited by Korhedron; 12-23-2002 at 01:56 PM.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    srand(time(NULL)) is the seed for the random number generator. Without your random number sequence is likely to be the same sequence each time you run the program.

    %d is C syntax for "insert an integer here" in the input/output with the integer to insert being the value after the comma.

    return low + (rand() % (high - low + 1));

    should work for a random number between high and low, inclusive, but only a run of the code will prove it.

  9. #9
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Can I use something else than %d ? Something C++...

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    printf("A random number from 0 to 99: %d", rand() % 100);

    becomes:

    cout << "A random number from 0 to 99 " << rand_mid(low, high);

    Need to use this line:

    #include <iostream.h>

    or these

    #include <iostream>

    using namespace std;
    OR
    using std::cout;
    OR
    std::cout << whatever; every time you use cout.

  11. #11
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    How come no-one seems to use randomize() and random() these days from stdlib.h. Are they just not part of the standard and are therefore just not used?
    Be a leader and not a follower.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How come no-one seems to use randomize() and random() these days from stdlib.h.
    Because random and randomize are specialized compiler extensions, and therefore nonstandard.

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

  13. #13
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    what ever you do.. computer generated numbers are not truly random.. The algorithm may be complex but still the number is not eandom.. To truly generate a random number you have to take input from the environment such as noise, temp etc etc etc and then using some algorithm convert those into a number......

  14. #14
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    920
    lol, in that case, nothing is truly "random" because there is a logical cause for every event in this universe, saying one can manipulate lone molecules.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  15. #15
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Thanks a lot...

    About rand_mid(low,high), I can use anything as rand_mid, or does it have to be called rand_mid ? Can I use random(low,high) ?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-12-2003, 12:03 AM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM

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