1

I want to simulate a random deliver of 52 standard cards without using rand/srand urandom etc...

this is my random number function

int rand2(int lim)
{
  static int a = 34;  // could be made the seed value                                                            
  a = (a * 32719 + 3) % 32749;
  return ((a % lim) + 1);
}

the struct where i will know if a card have already popped ( 0 = NO, 1 yes )

typedef struct          s_game
{
  int                   *cards;
  int                   state;
  unsigned int          dat_rand;
}                       t_game;

int             main()
{
  t_game        game;
  int           i;
  int           rd;

  i = 0;
  game.cards = malloc(sizeof(*game.cards) * 52);
  while(i < 52)
    {
      rd = rand2(52);
      if(game.cards[rd] == 0)
        {
          game.cards[rd] = 1;
          printf("i:%d\n rd: %d\n", i, rd);
          i++;
        }
    }
}

But my output is always the same, each cards is delivered at the same time so im searching fo a better random function or a different way to fill my deliver

Saxtheowl
  • 4,136
  • 5
  • 23
  • 32
  • 2
    What makes you think that your function is any more random than the standard functions? – JJJ Sep 30 '13 at 09:13
  • 3
    Did googling **["random number c"](http://stackoverflow.com/questions/822323/how-to-generate-a-random-number-in-c)** yield no results? I don't believe that... – ppeterka Sep 30 '13 at 09:13
  • of course, but I want my own random function – Saxtheowl Sep 30 '13 at 09:15
  • 2
    @Saxtheowl why? There is a lot of high-maths about randomness. Doing it yourself will surely result in a non-random number. (Unless you do have a PhD in mathematics, but then you wouldn't be asking this question here). – Bart Friederichs Sep 30 '13 at 09:17
  • 3
    Possible duplicate: http://stackoverflow.com/questions/6275593/how-to-write-you-own-random-number-algorithm **Don't invent your own crypto/prng!** – Alasjo Sep 30 '13 at 09:18
  • @Saxtheowl, your implementation of the random function is quite basic. `rand()` is either the same or a better random algorithm. Furthermore, I'd like to remind you that the more significant bits of the random number or "more random" than the lower bits. So instead of using `%`, try `* lim / RAND_MAX` (taking care of edge cases). – Shahbaz Sep 30 '13 at 09:18
  • 1
    @Shahbaz "more significant bits [are] more random" Got a link to info to back that claim up? Don't doubt it, just interested. Nevermind I found something: http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx – weston Sep 30 '13 at 10:28
  • @Shahbaz Reading around, `*lim/RAND_MAX` is not a good solution: http://www.azillionmonkeys.com/qed/random.html – weston Sep 30 '13 at 10:41
  • @weston, very interesting read. For single line `rand()` usage, `*lim/RAND_MAX` is still an improvement over `%lim`, even though it's not great. Call it a quick improvement. Of course, for better results much more can be done. Thanks for the link. – Shahbaz Sep 30 '13 at 11:52

1 Answers1

6

Of course your sequence of numbers will always be the same, computers are extremely deterministic at this level.

To improve your generator, "seed" it using e.g. the current up-time of the computer, that's a classic way. It will still generate the same sequence as long as the seed is the same, of course.

The better fix is to "cryptographically secure pseudorandom numbers", which are not part of C so you have to do something platform-specific to get hold of some. Not sure why you say that you don't "want" to use that; you kind of have to to get proper random numbers.

Also, because computers are so deterministic, generating "true" (or just "good") random numbers is pretty hard. It's not reasonable to expect that you're going to solve it yourself, especially since (no offense) it seems you're quite new to programming in C. Personally I would certainly hesitate for a long while before attempting to roll my own secure random generator.

unwind
  • 391,730
  • 64
  • 469
  • 606