Welcome, Guest. Please login or register.

Author Topic: lame question about rand() :)  (Read 4351 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 kas1eTopic starter

Re: lame question about rand() :)
« Reply #1 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 kas1eTopic starter

Re: lame question about rand() :)
« Reply #2 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 kas1eTopic starter

Re: lame question about rand() :)
« Reply #3 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 kas1eTopic starter

Re: lame question about rand() :)
« Reply #4 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 ?