mathematical random distribution

This is a discussion on mathematical random distribution within the C++ Programming forums, part of the General Programming Boards category; I'm looking for an example about constructing data to be random (I know about the rand() function) but the data ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    1

    mathematical random distribution

    I'm looking for an example about constructing data to be random (I know about the rand() function) but the data should be distributed in different ways (evenly, accelerating)- thanks for helping...and excuse my english

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    your english is fine : )

    Hope this helps, i rlly don't kno much bout randomness:

    Code:
    /*
    
    // Name: Code Example - generating random numbers
    
    // Description:Today in class we went over a few different concepts. I wrote my own
    
    examples of these C++ concepts.
    
    // By: Jared Bruni
    
    //
    
    //
    
    // Inputs:None
    
    //
    
    // Returns:None
    
    //
    
    //Assumes:None
    
    //
    
    //Side Effects:None
    
    //This code is copyrighted and has limited warranties.
    
    */
    
    
    
    /* random number example
    
    written by Jared Bruni
    
    www.LostSideDead.com
    
    */
    
    #include<iostream>
    
    #include<time.h>
    
    using namespace std;
    
    
    
    int main()
    
    
    
    
    
        {
    
        	srand(time(NULL)); // seed randomization
    
        	// now generate and display 10 random numbers 0-10
    
        	for(int i = 0; i < 500; i++)
    
    
    
    
    
    		{
    
            		cout << "random number: " << rand()%10 << endl;
    
            }	
    
            	return system("pause");
    
        }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    This is a simple function that gives good distribution:
    Code:
    int nrand ( int n )
    {
      if ( n <= 0 || n > RAND_MAX )
        throw domain_error ( "Argument to nrand is out of range" );
    
      const int bucket_size = RAND_MAX / n;
      int r;
    
      do {
        r = rand() / bucket_size;
      } while ( r >= n );
    
      return r;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 09:16 AM
  2. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  3. random number with lognormal distribution?
    By carrie in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2001, 08:31 PM
  4. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 05:11 PM
  5. Replies: 5
    Last Post: 10-12-2001, 04:51 AM

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