Thread: rand() existing variables?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    4

    rand() existing variables?

    is it not possible to randomize already existing variables?

    char a="hi";
    char b="I am";
    char c="new to this";

    ?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your question doesn't really make that much sense.
    What is the actual problem you are trying to solve?

    [I can think of at least three different things that you MAY mean - but I'm not going to post any of them, as I'd probably get it wrong anyways].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    4
    I got X number of different variables...all decleared with Char.

    what I want is to "cout << variablex"

    so each time the program starts it will send 1 random of the variables to the screen, then shut down
    .

    sry if that didn`t make much sense

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Declare an array of them.
    Then generate a random subscript.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    so you said you have x char variables, and each time the program runs, you want one of the x char variables to print, and then exit?
    Code:
    int main()
    {
        char myChars[] = { 'a', 'b', 'c', 'd' };
        // seed random number generator
        int random = // call function to generate random number from 0 to 3 inclusive
    
        cout << myChars[random] << endl;
    }
    is this logic something that your looking for? again, to me this seems like what you are asking

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also use const char* for string constants or (const) std::string.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ignotus View Post
    I got X number of different variables...all decleared with Char.

    what I want is to "cout << variablex"

    so each time the program starts it will send 1 random of the variables to the screen, then shut down
    .

    sry if that didn`t make much sense
    You can't do THAT, but as suggested elsewhere, you can use an array and a random index to achieve the effect you want. But what you suggest involves runtime decisions on which variable to use, and the C or C++ languages do not support that.

    Edit: And of course, if you are REALLY desperate to not create an array of your variables, you could make an array of pointers to the original variables, e.g.
    Code:
    char *a = "Hi";
    char *b = "Hello";
    char *c = "'Morning";
    char **arr[] = { a, b, c };
    ...
    --
    Mats
    Last edited by matsp; 01-01-2009 at 02:01 PM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    4
    thanx for the replies.
    I did load them into a array and then randomized them.

    worked wonders

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 09:01 AM
  5. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM