Help with Random...

This is a discussion on Help with Random... within the C++ Programming forums, part of the General Programming Boards category; Can I get a random value from an array? Example: Code: char vals[5]={'3','350','400','44','2'}; then make it print out 3, 350, ...

  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    Question Help with Random...

    Can I get a random value from an array?

    Example:

    Code:
    char vals[5]={'3','350','400','44','2'};
    then make it print out
    3, 350, 400, 44, or 2 at random?
    What is C++?

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    use rand()%5 to give you the numbers 0 to 4. Then grab that element from your array.

  3. #3
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    Yes

    You can get a random value from an array. What I would do is randomize an Integer from 0 to (whatever index) then just access that array using index X. So like: val[x]; where x will be your randomized variable.

  4. #4
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    sure,
    Code:
    rand() % array_size_-1
    do you know about the rand() function, seeding, and all that stuff. if not check the faq. did that answer your question?

  5. #5
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    like this?

    Code:
    char test[5]={'2','5','20','38','24'};
    
    srand(time(NULL));
    test = rand() % 4;
    
    cout<<test;
    ????
    If thats not right pls give example
    What is C++?

  6. #6
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    You got it!

    Yes that is a correct method of randomization. However that will not display the index. Here is how I would do it:

    #include "time.h"

    ...
    INT x;
    srand(time(NULL));
    x = rand()%4;
    cout<<test[x];

  7. #7
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    NICE

    Sweet... thx for the quick help all.
    What is C++?

  8. #8
    Unregistered
    Guest
    Code:
      char test[5]={'2','5','20','38','24'};
      int x;
    
      srand((unsigned)time(NULL));
      x=rand()%5;
      cout<<test[x];

  9. #9
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    rand()%4 will return 0-3. If you have 4 items in your array use rand()%5

  10. #10
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    but.....

    If I have 5 items in an array, it should be
    rand()%4

    that will get 0 - 4

    0, 1, 2, 3, 4, <--- That is 5 numbers

    What is C++?

  11. #11
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    Nope, %4 will give 0,1,2,3. Modulus is the remainder of the fraction.

    1/4 = 0 with 1 as the modulus
    2/4 = 0 with 2 as the modulus
    3/4 = 0 with 3 as the modulus
    4/4 = 1 with 0 as the modulus
    5/4 = 1 with 1 as the modulus
    6/4 = 1 with 2 as the modulus

    etc . . .
    I doubt, therefore I might not be

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

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