Thread: perfect randomness?

  1. #16
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by Lurker
    345
    7
    19
    44
    .5
    56,023.3
    3.3

    That isn't truly random? I would love to see a pattern for that.
    Here's an equation for those numbers:

    -467.25x^6 + 7475.1x^5 - 44369x^4 + 121333x^3 - 150883x^2 + 66573x + 345

    Just use x=0,1,2,3,4,5,6 and you get exactly those numbers out.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  2. #17
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686

    I program with random numbers pretty often... I haven't seen any problems with using rand() yet... as long as you seed it, it should be fine... unless there's some kind of portablitiy problem or something...
    It is not portability problems (though, rand() isn't guaranteed to be defined the same for all implementations). The problem is that the distribution of the numbers and other related statistics are generally poor (again, it's not the same for every implementation). For scientific or security applications, rand() is generally a poor choice because you may need a certain distribution, or you may need more randomness. If I remember correctly, many rand() implementations have relatively high correlation in the low order bits (that is, those bits are "less random").

  3. #18
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36

    Question

    what is eexactly the diff btw rand() and srand()?

  4. #19
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Lurker
    345
    7
    19
    44
    .5
    56,023.3
    3.3

    That isn't truly random? I would love to see a pattern for that.
    But they weren't calcuated by the rand() function. That's what is being discussed here.

  5. #20
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36

    Question

    what is seed? and diff btw rand() and srand()?

  6. #21
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    srand() seeds the random number generator... usually you see srand(time(0)); near the top of a program that uses rand(). rand() returns a 'random' integer, and srand() seeds it... I'm not too sure how rand() works... I've heard different stories about it, but I think it uses the seed in some kind of calculation to get to the random number or something... IDK... using time(0) uses the current time as the seed, so it will be more random...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #22
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    rand() is generally a linear congruential generator. It works basically like this:

    x[i + 1] = (a * x[i] + c) % m

    Where a, c, and m are constants, and the x's are the "random" numbers.

  8. #23
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    but where are the 'random' numbers comming from in the first place... and where are the constants defined?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #24
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36
    Thanku. Its a very complicated topic.

  10. #25
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The constants are implementation defined. The first x is the seed value, which is then replaced by the next x. I am doing a horrible job explaining this in English. Here is some example code (note, the constants are not chosen for any particular reason, so it will be a bad generator):

    Code:
    unsigned int seed;
    
    void srand(unsigned int s)
    {
      seed = s;
    }
    
    unsigned int rand()
    {
      seed = (37 * seed + 8) % 247;
      return seed;
    }

  11. #26
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36

    Thumbs down

    Well I was trying to generate some random numbers here but it seems that the values generated by the following code are not within my defined limit?

    Code:
    #include<iostream>
    #include<conio.h>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    void genNum(void);	// prototype
    
    int main()
    {
    	genNum();	// function call
    }
    void genNum()	// definition
    {
    	int value1, value2, result, sum;	// declare alias
    	
    	cout<< value1 << "+" << value2 << "=\t";	// prompt the user for input
    	cin>> result;	// read input
    	value1=1+rand()%6;	// generate random integer 1-10
    	value2=1+rand()%6;	// generate random integer 1-10
    	sum=value1+value2;	// calculate sum
    
    	if(sum==result)
    	{
    			cout<< "Congratulations! correct";
    	}
    	else
    			cout<< "No. Please try again";
    }

  12. #27
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    BTW, for some reason (I think backwards compatibility) the MS implementation of rand is something like

    Code:
    unsigned int rand()
    {
      seed = (calculation);
      return seed & 0x8fff;
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #28
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Use the Boost library it has random number generators for all occaisions including MT my personal favourite for simulations.
    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

  14. #29
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    What's wrong with the following:

    Code:
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int x;
       
       // Gives the rand() function a seed based on the time
       srand( time( 0 ) );
       
       for( int i = 1; i <= 10; i++)
          cout << ( 1 + rand() % 100 ) << '\n'; // shifted and scaled integers produced by 1+rand()%100
                                                // number range from 1 - 100 
       
       cin.get();
       return 0;
    }

  15. #30
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    I think you need to include something like "time.h", "ctime" or maybe its just "time", try with including one of those.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My perfect number assignment...
    By Ninestar in forum C Programming
    Replies: 8
    Last Post: 11-16-2005, 02:47 AM
  2. Perfect Numbers C Program
    By SlayerBlade in forum C Programming
    Replies: 2
    Last Post: 08-28-2005, 05:11 PM
  3. Perfect Number Problem
    By TrazPFloyd in forum C++ Programming
    Replies: 21
    Last Post: 10-20-2002, 11:09 AM
  4. Perfect number
    By TheSki in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2001, 04:34 PM
  5. perfect squares?
    By ahalya in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2001, 09:38 PM