#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    32
    Rep Power
    10

    Random Number Generator Hatred


    i made a simple black jack type game, obviously dealing should be random so, when you hit new game it deals you both 2 cards, i cant tell what the computers second card is because your not supozed to know and i am too lazy to make any changes, so it almost always deals me a 4 and a 9 and the computers first card is almost always an Ace

    here is the code
    Code:
    {
    	int x, randSigMan = rand () % 300;
    	for (x = 0; x < 300; ++x)	{
    		randSigMan += rand() % 300;
    	}
    	return randSigMan;
    }

    What is a good way to get a freakishly random number?
    this seemed good in the code i do this (Obj-C)

    srand([self randomFunction]);

    so srand is given randSigMan
  2. #2
  3. Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jan 2004
    Location
    near St. Louis Illinois
    Posts
    3,288
    Rep Power
    24
    call srand(time(0)) on program startup. that will set the seed to some random value based on current time/date. only call srand() once, soon after main() is usually a good place.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    32
    Rep Power
    10
    thanks, thats much better, it was annoying getting the same thing everytime
  6. #4
  7. Advanced Programmer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Malaysia
    Posts
    452
    Rep Power
    41
    rand() only generates the next number in a pseudorandom number sequence, hence why you need to seed it so that it always starts at a different point.

    If you had a real random number generator (probably via some hardware device), you would not need to seed the number.

    You can also seed using any number you like , for example , try seeding using the time difference between keystrokes measured on a milisecond level.

IMN logo majestic logo threadwatch logo seochat tools logo