Amiga.org
Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: kas1e 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 ?
-
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, ... .)
-
10 points for the perfect answer. :-)
-
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.
-
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?
-
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
-
Duh. Hadn't realised that as we're not running a loop, but yes, Thomas is correct.
-
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 ?
-
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):
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!
-
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:
#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("%d\n",a);
count++;
};
}
and was have this result:
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 ?:)
-
Well, a single statement
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 :).
-
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:
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 ?
-
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.
-
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.