QJ.NET | Videos | Forums | iPhone | MMORPG | Nintendo DS | Wii | PlayStation 3 | PSP | Xbox 360 | PC | Downloads | Contact Us
Forums | Gaming News | Videos | Downloads | Today's Posts | Mark Forums Read | Chat | FAQ | Members List | Contact

QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides

Go Back   QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides > Developers Corner > PSP Development, Hacks, and Homebrew > PSP Development Forum
The above video goes away if you are a member and logged in, so log in now!

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?..?...

Reply
 
LinkBack Thread Tools
Old 09-30-2006, 09:50 PM   #1
likes kittens....awww....
 
Join Date: Sep 2006
Real First Name: Erik
Location: Detroit
Just Played: Call of Duty:World at War
Posts: 628
Trader Feedback: 0
Default generating random nums??

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

Last edited by psphacker12.; 09-30-2006 at 10:06 PM..
psphacker12. is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 09-30-2006, 09:56 PM   #2
 
Join Date: Sep 2006
Posts: 121
Trader Feedback: 0
Default

ohh! make an xbox live code gen!
UB3R-W4N-K0N0B1 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 09-30-2006, 09:57 PM   #3
 
Twin891's Avatar
 
Join Date: Jul 2006
Location: Canada
Posts: 70
Trader Feedback: 0
Default

There is a C/C++ help thread you know...
Twin891 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 09-30-2006, 10:07 PM   #4
likes kittens....awww....
 
Join Date: Sep 2006
Real First Name: Erik
Location: Detroit
Just Played: Call of Duty:World at War
Posts: 628
Trader Feedback: 0
Default

Quote:
Originally Posted by UB3R-W4N-K0N0B1
ohh! make an xbox live code gen!
No requests..
psphacker12. is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 09-30-2006, 10:08 PM   #5
 
Join Date: Sep 2006
Posts: 121
Trader Feedback: 0
Default

huh? it was a suggestion.
UB3R-W4N-K0N0B1 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 09-30-2006, 10:31 PM   #6
 
Soulphalanx's Avatar
 
Join Date: Sep 2005
Location: TROY
Posts: 2,989
Trader Feedback: 0
Default

#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
__________________
[IMG]http://img121.imageshack.us/img121/3852/untitled1copy7rf.png[/IMG] [IMG]http://img99.imageshack.us/img99/9711/newavatarjb9.gif[/IMG]


[URL=http://forums.qj.net/showthread.php?t=9733][COLOR=DarkRed]Operation Positive Posting[/COLOR][/URL] [URL=http://forums.qj.net/showthread.php?t=49762][COLOR=Red]Guide to All Guides[/COLOR][/URL]
[URL=http://forums.qj.net/showthread.php?t=13798][COLOR=Blue]Posting Guidelines[/COLOR][/URL] [URL=http://forums.qj.net/showthread.php?t=4394][COLOR=Green]Piracy Policy[/COLOR][/URL]

[SIZE="3"][URL=http://forums.qj.net/f-general-psp-help-22/t-guide-opening-psp-disassembly-assembly-video-104303.html][COLOR=Red][B][U]PSP Disassembly Assembly Video Tutorial[/U][/B][/COLOR][/URL][/SIZE]
Soulphalanx is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 12:28 AM   #7

...in a dream...
 
SG57's Avatar
 
Join Date: Jul 2005
Posts: 4,957
Trader Feedback: 0
Default

Gahhh.... read the rules psphacker, 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...
__________________
SG57 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 04:51 AM   #8

AKA Homer
 
Moonchild's Avatar
 
Join Date: Jan 2006
Location: Sweden
Posts: 1,779
Trader Feedback: 0
Default

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

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


Click Here if you want a Winamp Currently Playing Userbar like the one above.
Moonchild is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 05:06 AM   #9

OMFG
 
Slasher's Avatar
 
Join Date: Jul 2005
Location: Toronto
Posts: 2,816
Trader Feedback: 0
Default

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
Slasher is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 06:41 AM   #10

Developer
 
yaustar's Avatar
 
Join Date: Jun 2006
Location: UK
Posts: 2,317
Trader Feedback: 0
Default

rand() is pseudo anyway. The only 'random' factor used is the seed used with srand(). http://www.random.org/
yaustar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 07:48 AM   #11

OMFG
 
Slasher's Avatar
 
Join Date: Jul 2005
Location: Toronto
Posts: 2,816
Trader Feedback: 0
Default

http://forums.qj.net/f-psp-developme...ion-39517.html

Check this out too.

Last edited by Slasher; 10-01-2006 at 08:12 AM..
Slasher is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 07:58 AM   #12

SHOOP DA WHOOP
 
mannymix03's Avatar
 
Join Date: Aug 2005
Location: Wii forums
Posts: 7,404
Trader Feedback: 0
Default

too bad its really not random as a computer cant have a random number
__________________
Required ReadingPiracy Policy |Positive Posting

mannymix03 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 08:12 AM   #13

Give It All
 
Join Date: Jan 2006
Real First Name: Derek
Location: United States
Just Played: Burnout Paradise
Posts: 3,899
Blog Entries: 1
Trader Feedback: 0
Default

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

Core 2 Duo E8400 @ 3.6ghz (Multiplier: 9x; Bus: 400mhz; FSB: 1600mhz;)
GeForce 9800 GTX+ 512 MB (Core: 850mhz; Memory: 1200mhz; Shader: 2100mhz;)
ASUS P5K Pro Motherboard
Patriot Extreme (2x2GB) / OCZ HPC (2x1GB)
6GB Total Dual Channel RAM (DDR2 1066mhz)
OCZ GameXstream 700w
2x Seagate 250GB HD (RAID0)
GmDude66 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 08:12 AM   #14

OMFG
 
Slasher's Avatar
 
Join Date: Jul 2005
Location: Toronto
Posts: 2,816
Trader Feedback: 0
Default

Quote:
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.
Slasher is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 08:16 AM   #15
 
FreePlay's Avatar
 
Join Date: Dec 2005
Location: h0000000rj
Posts: 12,858
Trader Feedback: 0
Default

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
__________________
[qj now fails.]
FreePlay is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 08:20 AM   #16

OMFG
 
Slasher's Avatar
 
Join Date: Jul 2005
Location: Toronto
Posts: 2,816
Trader Feedback: 0
Default

Quote:
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.
Slasher is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 08:22 AM   #17

AKA Homer
 
Moonchild's Avatar
 
Join Date: Jan 2006
Location: Sweden
Posts: 1,779
Trader Feedback: 0
Default

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


Click Here if you want a Winamp Currently Playing Userbar like the one above.
Moonchild is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 08:34 AM   #18

SHOOP DA WHOOP
 
mannymix03's Avatar
 
Join Date: Aug 2005
Location: Wii forums
Posts: 7,404
Trader Feedback: 0
Default

Quote:
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
__________________
Required ReadingPiracy Policy |Positive Posting

mannymix03 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 08:37 AM   #19

Give It All
 
Join Date: Jan 2006
Real First Name: Derek
Location: United States
Just Played: Burnout Paradise
Posts: 3,899
Blog Entries: 1
Trader Feedback: 0
Default

Quote:
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..
__________________

Core 2 Duo E8400 @ 3.6ghz (Multiplier: 9x; Bus: 400mhz; FSB: 1600mhz;)
GeForce 9800 GTX+ 512 MB (Core: 850mhz; Memory: 1200mhz; Shader: 2100mhz;)
ASUS P5K Pro Motherboard
Patriot Extreme (2x2GB) / OCZ HPC (2x1GB)
6GB Total Dual Channel RAM (DDR2 1066mhz)
OCZ GameXstream 700w
2x Seagate 250GB HD (RAID0)
GmDude66 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 08:40 AM   #20

SHOOP DA WHOOP
 
mannymix03's Avatar
 
Join Date: Aug 2005
Location: Wii forums
Posts: 7,404
Trader Feedback: 0
Default

Quote:
Originally Posted by GmDude66
Who the hell would want to do that..
no one, but i mean. Its not perfectly random
__________________
Required ReadingPiracy Policy |Positive Posting

mannymix03 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 11:11 AM   #21
 
pspcoder05's Avatar
 
Join Date: Sep 2006
Posts: 167
Trader Feedback: 0
Default

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);
sceKernelSleepThread();
return 1;
}
See....pretty simple huh?
pspcoder05 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 11:30 AM   #22

...in a dream...
 
SG57's Avatar
 
Join Date: Jul 2005
Posts: 4,957
Trader Feedback: 0
Default

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...
__________________
SG57 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 11:34 AM   #23


Developer
 
Xfacter's Avatar
 
Join Date: May 2006
Posts: 114
Trader Feedback: 0
Default

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...
__________________

For game downloads and development updates, check out my website.
http://xfacter.wordpress.com/
Xfacter is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 11:40 AM   #24

...in a dream...
 
SG57's Avatar
 
Join Date: Jul 2005
Posts: 4,957
Trader Feedback: 0
Default

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...
__________________
SG57 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-01-2006, 10:44 PM   #25
 
pspcoder05's Avatar
 
Join Date: Sep 2006
Posts: 167
Trader Feedback: 0
Default

Wooh....
you really code well.
pspcoder05 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-02-2006, 12:14 AM   #26
Art

Bush Programmer
 
Art's Avatar
 
Join Date: Nov 2005
Posts: 3,557
Trader Feedback: 0
Default

Quote:
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.
Art is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
generating , nums , random

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -8. The time now is 09:58 AM.



Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY
Copyright © 2009, QJ.NET. All Rights Reserved.
Contact Us