Randomize??????

This is a discussion on Randomize?????? within the C++ Programming forums, part of the General Programming Boards category; Hello I was wondering how do I randomize a value, I need this to decide random outcomes (obviously) thanks for ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    75

    Randomize??????

    Hello


    I was wondering


    how do I randomize a value, I need this to decide random outcomes (obviously) thanks for any help simple code will do me

    cheers

    stealth

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    8
    You can use the rand() function in cstdlib to get a random number.

    It is used like this:

    Code:
    int x = rand() % 5; //x would be between 0 and 4
    Before calling rand(), you should call srand() to seed the random number generator. You must always use a different number when calling srand(), the current time for instance, or else rand() will always give you the same numbers.

    So you might want to do something like this:

    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    
    using namespace std;
    
    int RandInt(int a,int b)
    {
       return a + rand() % (b - a + 1);
    }
    
    void main()
    {
       //Seed rand() with current time
       srand(unsigned(time(NULL)));
       //x will be between 2 and 10, inclusive
       int x = RandInt(2,10);
       cout<<x;
    }

  3. #3
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    There is more information of randomizing in the FAQ.
    Making error is human, but for messing things thoroughly it takes a computer

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    75

    ok

    ok thanks for the help guys

    cheers

    stealth

  5. #5
    Unregistered
    Guest
    I've tried doing random numbers using that same method and wanted to do random numbers between 1 and 100.

    here are my results
    2, 9,12,19,24, ..... etc, etc you get the idea I guess. There always increasing. Not really random. Is there anyway to get around this?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    Thumbs up

    Yea there is you could do this


    random{int n ) {


    static bool seeded = false; /* used to ensure seeding done just one */

    int seed;


    if(!seeded) {

    cout <<"Enter a value to randomize a value" << endl;
    cin>> seed;
    cin.ignore(80,'\n');
    srand(seed);
    seeded = true;
    }

    * then you call your rand() */

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    3
    cout <<"Enter a value to randomize a value" << endl;
    cin>> seed;
    cin.ignore(80,'\n');
    srand(seed);
    seeded = true;

    don't work in a visual C++ Dialog window or what ever you want ever there called. And I don't really want to have to enter a value every time I run it. Is there anyother way to do what I want?

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    8
    If you want different seeds everytime you could use the current time. So you would do something like this:

    #include <time.h>

    srand(unsigned(time(NULL)));

    Then you will get different numbers from rand() every time without having to specify a seed value.

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    3
    That just leads back to the same problem in the fifth post. So I might as well assume that there is no way to actually generate random numbers in C++?

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I've tried doing random numbers using that same method and wanted to do random numbers between 1 and 100.

    here are my results
    2, 9,12,19,24, ..... etc, etc you get the idea I guess. There always increasing. Not really random. Is there anyway to get around this?
    This should not happen. Check the rest of your program.
    Maybe you can post the code that lead to these pseudo-random
    results.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    This is all in the frequently asked questions but some of you dont appear to even read that so here goes again....

    RANDOM NUMBERS.

    To get original random numbers first we have to seed the random number generator. This is commonly done by seeding with the system time. This only has to be done once for every run of your app.

    you do it like this...
    Code:
    #include<ctime> // or time.h
    #include<cstdlib> // or stdlib.h
    using namespace std; // omit this line if using older headers
    int main()
    {
    srand((unsigned)time(NULL)); // this seeds the rand generator.
    return 0;
    }
    ok thats the seeding done. Now lets see if we can generate 10 random numbers in the range of 1-100.This requires the use of rand(). This function returns a pseudorandom integer between 0 and RAND_MAX.To cut this range down into something we need (i.e. 1-100) we need to use the modulus operator (%) to bring the number into range. If we say rand()%100 this will give us a random number between 0 and 99. To change this to 1-100 just add 1. So lets put that together in some code.
    Code:
    #include<ctime> // or time.h
    #include<cstdlib> // or stdlib.h
    #include<iostream> // or iostream.h
    using namespace std; // omit this line if using older headers
    int main()
    {
    srand((unsigned)time(NULL)); // this seeds the rand generator.
    int num=0;
    for (int i=0;i<10;i++)
    {
    num=rand()%100+1;
    cout<<num<<endl;
    }
    return 0;
    }
    One last thing as you have seen rand() produces ints so if you need a random float/double you can get a random int in the normal way and divide it by RAND_MAX+1.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. randomize array blackjack c program
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-01-2005, 03:05 PM
  2. don't want to randomize two alike numbers
    By randomizer in forum C++ Programming
    Replies: 8
    Last Post: 05-26-2005, 08:42 PM
  3. randomize hex digits
    By wazilian in forum C Programming
    Replies: 3
    Last Post: 12-14-2002, 03:20 AM
  4. Problem with Visual C++ ( randomize function )!
    By marCplusplus in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2001, 01:01 PM
  5. Randomize Number Function
    By beyonddc in forum C Programming
    Replies: 3
    Last Post: 12-10-2001, 04:31 AM

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