Welcome, Guest. Please login or register.

Author Topic: lame question about rand() :)  (Read 4347 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show all replies
Re: lame question about rand() :)
« on: March 31, 2006, 03:49:24 PM »
rand() returns a number between 0 and RAND_MAX, so you would want something like

a = 1 + (int)(3.0*rand()/(RAND_MAX+1.0));

for an integer between 1 and 3 inclusive. If you want between 0 and 3, use

a = (int)(4.0*rand()/(RAND_MAX+1.0));

because 4.0*rand() is at most 4*RAND_MAX, so when you divide by RAND_MAX+1.0, you get a number which is slightly less than 4 at most, and thus rounded down to 3.

Don't use code like

a = 1 + (int)(rand() % 3);

because it emphasises a subset of bits of the random number, and thus is inherently less random. (Think of the rules you learned in primary school to determine whether a number is divisible by 2, 3, 4, ... .)
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show all replies
Re: lame question about rand() :)
« Reply #1 on: March 31, 2006, 04:38:02 PM »
Odd. The code won't compile without the necessary #include's---you require a few at least (stdio.h, stdlib.h, stddef.h, perhaps unistd.h too, I sort-of-forgot ever since I began using a modular code library  :-P) but if they weren't included...

Try and print out a few rand()'s in a loop, without any multiplications; include RAND_MAX too. Perhaps SAS/C is using a different convention for the random functions---these things are a royal mess, and it wouldn't be the first time something platform-dependent cropped up.

Oh, and what libraries are you linking with?
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show all replies
Re: lame question about rand() :)
« Reply #2 on: March 31, 2006, 04:57:23 PM »
Duh. Hadn't realised that as we're not running a loop, but yes, Thomas is correct.
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show all replies
Re: lame question about rand() :)
« Reply #3 on: March 31, 2006, 08:48:29 PM »
Quote
kas1e wrote:
In other words, i have in my code fucntion, which do some visual effects (here is 4 effect). And want to do always different effect. So, i need to generate always rand numbers between 0 and 3, and put rezult to my effect_do_function as integer value. of course will be good if it will compiller/platform independed. Any ideas ?

I'm not sure I understand your question. You now have a random generator which yields a random number between 0 and 3. But now you want to use that number to select a visual effect, and make sure that you don't get the same effect twice in a row. Is that correct? (In other words, 0-1-2-3-0-1-3-1-2 is okay, 0-1-2-3-2-1-2-2 is not.)

Doesn't sound too complicated (assuming a function my_rand() which returns the random number):

Code: [Select]
void do_visual_effect()
{
    static int old_effect;
    int effect;

    do {
        effect = my_rand();
    } while (effect == old_effect);

    old_effect = effect;

    // rest of code producing visual effect 'effect'...

    return;
}


Hope this helps!
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show all replies
Re: lame question about rand() :)
« Reply #4 on: April 01, 2006, 07:48:28 AM »
Well, a single statement
Code: [Select]
srand(time(NULL)); at the beginning of your code (say, the first executable statement of main()) should suffice. Don't go about calling srand() with every instance of rand()---that screws up the random generator.

Also keep in mind that you cannot conclude on the basis of just 10 numbers that the generator isn't random. Don't worry, it works :).
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show all replies
Re: lame question about rand() :)
« Reply #5 on: April 01, 2006, 09:39:52 AM »
Repeat the loop, say, 10000 times. (Pause between 1 and 2 seconds between each run with Delay().) If every time you run it the sequence starts with a 2, I'll be more inclined to say that the random generator is broken and that you need another one.
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.