The above video goes away if you are a member and logged in, so log in now!

 Would you like to get all the newest Gaming News fromQJ.NET in your email each day? Want to learn more about the team who brings you the QJ news?

## generating random nums??

This is a discussion on generating random nums?? within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; i know how to do this in lua but how do i do this in C?..?...

 Tweet
1. ## generating random nums??

i know how to do this in lua but how do i do this in C?..?

2. ohh! make an xbox live code gen!

3. There is a C/C++ help thread you know...

4. Originally Posted by UB3R-W4N-K0N0B1
ohh! make an xbox live code gen!
No requests..

5. huh? it was a suggestion.

6. #include <stdlib.h>
i = rand();

Where:

int i;
is a pseudo-random number in the range 0 to RAND_MAX (where RAND_MAX is a manifest defined in <stdlib.h>).

use

7. Gahhh.... read the rules psp, search atleast a little bit... if all else fails, theres a thread for this, not to meantion its been answered many many times... Next youll probably ask how to create a random numbers between 2 values - start and end - which has been answered many times before in the help thread, as well as when searching google...

Putting in 'random number in C' gave me 118,000,000 using google...

8. In your main where you intialize everything call
srand(time(NULL));

Then when you want a random number call
rand();

9. I think he's looking for a more specific random number generator. Not some pseudo number.

Code:
```#include <time.h>

srand(sceKernelLibcTime(NULL));

/* Get random number */
int getRandomNum(int min, int max)
{
int diff = max - min;
int res = (rand() % diff)+min;
return res;
}```
Should work

10. rand() is pseudo anyway. The only 'random' factor used is the seed used with srand(). http://www.random.org/

11. too bad its really not random as a computer cant have a random number

12. Originally Posted by mannymix03
too bad its really not random as a computer cant have a random number
Big deal, it's close enough :Argh:

13. Originally Posted by mannymix03
too bad its really not random as a computer cant have a random number
seeding it does infact make it random.

14. No, it doesn't, because the sequence of numbers generated will be the same each time you use the same seed. It's pseudo-random based on a mathematical sequence

15. Originally Posted by FreePlay
No, it doesn't, because the sequence of numbers generated will be the same each time you use the same seed. It's pseudo-random based on a mathematical sequence
Well...yea, I guess. Damn you.

16. That's why you seed it with time(NULL). The time is always increasing, so it should never be the same.

17. Originally Posted by homer
That's why you seed it with time(NULL). The time is always increasing, so it should never be the same.
but its still using a formula to derive it, if you really wanted you could figure it out and be able to tell the time from the random sequence

18. Originally Posted by mannymix03
but its still using a formula to derive it, if you really wanted you could figure it out and be able to tell the time from the random sequence
Who the hell would want to do that..

19. Originally Posted by GmDude66
Who the hell would want to do that..
no one, but i mean. Its not perfectly random

20. You can also specify the limit.
See....
Just use simple rand() function.
Code:
```..
....
.....

int main(void)
{
pspDebugScreenInit();
SetupCallbacks();
int rn; //random number holder
rn=rand()%100;//Where rand generates a random number inside 100;
pspDebugScreenPrintf("Generated Integer:%d",rn);
return 1;
}```
See....pretty simple huh?:)

21. Dont you think it'd be best to make the seed sceKernelLibcClock(void );? that returns the # of miliseconds since hte app has started... Seems better than time being seconds...

22. http://www.psp-programming.com/dev-f...pic.php?t=1076

Make sure to look at the last post if you want it to work right...

23. Ok, the truth ehind rand():

Code:
```static unsigned long _seed = 0;

long
rand(void)
{
if (_seed == 0)
_seed = 1;

if ((((_seed << 3) ^ _seed) & 0x80000000L) != 0)
_seed = (_seed << 1) | 1;
else
_seed <<= 1;

return _seed - 1;
}

void
srand(unsigned long new_seed)
{
_seed = new_seed;
}```
Man Im good...

24. Wooh....
you really code well.
:)

25. too bad its really not random as a computer cant have a random number
There is noise in the audio input buffer even when no microphone or remote
are connected.
It would be influenced by EMI from the environment, so not truly random,
but the best seed for random numbers on the PSP so far.

You can see the noise by starting this app:
http://home.arcor.de/themirek/pspdev/audioinput.zip
Press Circle and then square.
You don't need to bother plugging a mic or remote in.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

All times are GMT -8. The time now is 03:03 PM.

Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY