Forums | developer.brewmp.com Forums | developer.brewmp.com

Developer

Forums

Forums:

How do I get a random number between 1 and X for example
X being an integer.
example:
I would like to get a random number between 1 and 5 ?
Please someone give me a direct example that I can use on my code.

thanks

Try:
GETRAND( &var, sizeof(var) );
And then, take a look at modulus ( % ) function, in your case (var % 5)+1.

Try:
GETRAND( &var, sizeof(var) );
And then, take a look at modulus ( % ) function, in your case (var % 5)+1.

I think there is a function that produces random number based on the random generator on the device.
Here is the description:
void GetRand(byte * pDest, int nSize)
Quote:This function uses the random number generator on the device to fill an input buffer with a series of random bytes. The sequence is seeded by the current time, so multiple calls may give identical results unless separated by at least several milliseconds. For best results, call it once per invocation of event handler function.
HTH

I think there is a function that produces random number based on the random generator on the device.
Here is the description:
void GetRand(byte * pDest, int nSize)
Quote:This function uses the random number generator on the device to fill an input buffer with a series of random bytes. The sequence is seeded by the current time, so multiple calls may give identical results unless separated by at least several milliseconds. For best results, call it once per invocation of event handler function.
HTH

It worked as I asked
Thanks bulach and rocklee

It worked as I asked
Thanks bulach and rocklee

can i use modulus operator directly.
do i have to include any libraries
iam using VC++

can i use modulus operator directly.
do i have to include any libraries
iam using VC++

No you cannot use % in BREW but it'll work on VC++.

No you cannot use % in BREW but it'll work on VC++.

samiam wrote:No you cannot use % in BREW but it'll work on VC++.
Could you please elaborate why we cannot use % in BREW?

samiam wrote:No you cannot use % in BREW but it'll work on VC++.
Could you please elaborate why we cannot use % in BREW?

RPDP wrote:Could you please elaborate why we cannot use % in BREW?
Actually it is safe not to use % operator as there are some compilers that does not support MOD operation.
Right Now we're using ADS v1.2 & it does not support % operation. It throws an error with % operations whereas WINARM goes fine with % operator.

RPDP wrote:Could you please elaborate why we cannot use % in BREW?
Actually it is safe not to use % operator as there are some compilers that does not support MOD operation.
Right Now we're using ADS v1.2 & it does not support % operation. It throws an error with % operations whereas WINARM goes fine with % operator.

what is other way then
i mean how can we perform this operation without using % operator

what is other way then
i mean how can we perform this operation without using % operator

I am not sure why samiam has had issues with the modulo (%) operator, but I have never had any issues using it with the ADS 1.2 compiler before. I guess I would suggest you just try it out and see if it works for you unless samiam comes back and tells us why it doesn't work (maybe there are issues with how it is implemented in ADS that I just don't know about).

I am not sure why samiam has had issues with the modulo (%) operator, but I have never had any issues using it with the ADS 1.2 compiler before. I guess I would suggest you just try it out and see if it works for you unless samiam comes back and tells us why it doesn't work (maybe there are issues with how it is implemented in ADS that I just don't know about).

We have been using % (modulo operator) in our applications without any problems till now and we use RVCT for BREW v1.2. That's why I was curious to know why we should not use %.
Maybe, as Greg said, if there are any issues in the way ADS handles it, can anyone throw some light on it?

We have been using % (modulo operator) in our applications without any problems till now and we use RVCT for BREW v1.2. That's why I was curious to know why we should not use %.
Maybe, as Greg said, if there are any issues in the way ADS handles it, can anyone throw some light on it?

There is nothing wrong with the modulo operator on any compiler I have ever used -which is quite a few. There is certainly nothing wrong with the % operation on the ARM compiler. In general, if you want to claim there is a problem with a compiler, then the onus is on you to produce a detailed analysis of the problem - and if you want to claim that the problem lies in something as basic and fundamental as a built-in mathematical operation, then you had better be really sure of yourself.
If you try to do something module 0, then yes, you will get problems. Otherwise there is no issue.

There is nothing wrong with the modulo operator on any compiler I have ever used -which is quite a few. There is certainly nothing wrong with the % operation on the ARM compiler. In general, if you want to claim there is a problem with a compiler, then the onus is on you to produce a detailed analysis of the problem - and if you want to claim that the problem lies in something as basic and fundamental as a built-in mathematical operation, then you had better be really sure of yourself.
If you try to do something module 0, then yes, you will get problems. Otherwise there is no issue.