Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline kas1eTopic starter

lame question about rand() :)
« on: March 31, 2006, 03:26:55 PM »
Hi all. have lame question. for example i want to do
rand for ints like 1,2,3. How i can do it ?

i mean somethink like this:

int a;
// here is some setup tell that 'a' can be only from 0 to 3
rand(a);
printf("%d\n",a); // here is

can someone help a little with it ?
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show only replies by Cymric
Re: lame question about rand() :)
« Reply #1 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 Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: lame question about rand() :)
« Reply #2 on: March 31, 2006, 04:02:49 PM »
10 points for the perfect answer. :-)
 

Offline kas1eTopic starter

Re: lame question about rand() :)
« Reply #3 on: March 31, 2006, 04:06:41 PM »
Hm, maybe i do not understand somethink, but i try (sasc) :
#include ...
main()
{  
int a;
a = 1 + (int)(3.0*rand()/(RAND_MAX+1.0));
//or
//
//a = (int)(4.0*rand()/(RAND_MAX+1.0));  
printf("%d\n",a);
}

for first case , after running always 1.
for second case, after running always 0.
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show only replies by Cymric
Re: lame question about rand() :)
« Reply #4 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 Thomas

Re: lame question about rand() :)
« Reply #5 on: March 31, 2006, 04:42:02 PM »

rand() always starts at the same number. You have to call srand() in order to initialize the random number generator. Usually you will use srand(time(NULL)) to start at a somewhat real random number.

Bye,
Thomas

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show only replies by Cymric
Re: lame question about rand() :)
« Reply #6 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 kas1eTopic starter

Re: lame question about rand() :)
« Reply #7 on: March 31, 2006, 05:53:14 PM »
sorry for my lame, but more questions is here:

so, i need to do just this: programm which will print number from 0 to 3 by printf in random. I mean run = have 2 for example. Run agayn - 1. run agayn = 0, or 3, or maybe 1 agayn.

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 ?
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show only replies by Cymric
Re: lame question about rand() :)
« Reply #8 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 kas1eTopic starter

Re: lame question about rand() :)
« Reply #9 on: April 01, 2006, 07:20:30 AM »
thanks for help, but, problem it is not in this fucntion, problem in my_rand() fucntion. i want always different number between 0 and 3. But if i will use your uppear examples, i always will have on running stage the same number.

also i was try to do loop with 10 cicles:
Code: [Select]

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main()
{
   int a=0;
   int count=0;

   while(count <10)
   {
      a=1+(int)(3.0*rand()/(RAND_MAX+1.0));
      printf(&quot;%d\n&quot;,a);
      count++;
   };

}

and was have this result:
Code: [Select]

1
1
1
1
1
3
1
3
3
1

looks like not so randomize (where is 2?:) ), and if i run programm agayn, result is always the same. So, i must add in my_rand() and srand() too ?

At this moment i just interesting in little code, which i can compile on sasc, and after running of this programm, i always will have different numbers (and pretty random numbers) between 0 and 3. it is possible ?:)
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show only replies by Cymric
Re: lame question about rand() :)
« Reply #10 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 kas1eTopic starter

Re: lame question about rand() :)
« Reply #11 on: April 01, 2006, 09:30:29 AM »
well, with srand() it almost ok ! more randomize :) looks like different numbers always, but. For example from loop of 10 counts, first number - always the same. second numbers - changes some times, and other numbers really random always. But will be good to do 'real random' for all numbers (include first and second too).

and here is a results every 30 seconds:
Code: [Select]

 work:programming>rand
2211322311
 work:programming>rand
2221123132
 work:programming>rand
2331233323
 work:programming>rand
2313322211
 work:programming>rand
2132113132
 work:programming>rand
2211231223
 work:programming>rand
2232313121
 work:programming>rand
2321112312
 work:programming>rand
2333233211
 work:programming>rand
2122321132
 work:programming>rand
2131112223
 work:programming>rand
2211223111
 work:programming>rand
2332132312
 work:programming>

well, looks like first number always the same,  somethink between 3st-8st real random, and 9-10st random, but not so :) it is possible to add randomize for first number too ?
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show only replies by Cymric
Re: lame question about rand() :)
« Reply #12 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.
 

Offline 3246251196

  • Sr. Member
  • ****
  • Join Date: Mar 2006
  • Posts: 334
    • Show only replies by 3246251196
Re: lame question about rand() :)
« Reply #13 on: April 05, 2006, 03:33:11 AM »
everyone uses C still lol. i started learning C++ and it seems i am the only one who started at C++. not that there is too much of a difference. i just would have expected people to have moved away from C to C++ but i suppose more people are comfortable with what they have done longer.
******************