Skip to main content
GameDev.net gamedev.net
🔒 Locked

A Question about random numbers

Started by Cyberconvict Feb 8, 2002 at 10:45 PM 8 replies 1.1k views
Martee
Martee
The simple way:

srand((unsigned int) time(0));
int i = rand() % 3 + 1;
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
zealouselixir
zealouselixir
#include <stdlib.h>
#include <time.h>

This is a good method, though it's slower than the [sloppy] method above.

...
//initialization code
time_t t;
srand((unsigned int)time(&t));

...
//to generate a random number (between 1 and 3)
int RandInt = (int)((rand()/(float)RAND_MAX) * 3) + 1;
...

Later,
ZE.


//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

EDITed for code correction.

Edited by - zealouselixir on February 8, 2002 12:01:46 AM
[twitter]warrenm[/twitter]
a person
a person
btw the second method (div by RAND_MAX) is more random then the first method (mod) posted. since the second method uses all the bits versus the first method which only uses the low bits (in this case only 2 bits matter). though in the second method it should look more like:

//initialization code
// dont need to pass a strucr to time, NULL is
// an acceptable parm
srand((unsigned int)time(NULL));

...
//to generate a random number (between 1 and 3)
int RandInt = (int)((rand()/(float)RAND_MAX) * 3) + 1;



Edited by - a person on February 9, 2002 1:25:26 AM
EvilCrap
EvilCrap
..............................................................................................................................................................................................................................sad

randomNumber = (rand() % (MAX - MIN)) + MIN;

Edited by - evilcrap on February 9, 2002 3:54:16 AM
matrix2113
matrix2113
If you want better Random number generators, look up
psudo-random number generator c++
on any search engine. There are some much better ones (Mother of all Random, for instance) than what come with C++

"I''ve learned something today: It doesn''t matter if you''re white, or if you''re black...the only color that REALLY matters is green"
-Peter Griffin
"I've learned something today: It doesn't matter if you're white, or if you're black...the only color that really matters is green"-Peter Griffin
Beer Hunter
Beer Hunter
  
u_long random_org;
void InitRNG() {
WSAData wsaData;
WSAStartup(MAKEWORD(1, 1), &wsaData);
hostent* pHE = gethostbyname("www.random.org");
random_org = *((u_long*)pHE->h_addr_list[0]);
}
const char request_format[] =
"GET http://www.random.org/cgi-bin/randnum?num=1&min=%i&max=%i&col=1 HTTP/1.0\n\n";

char random_request[95];
int RandomNumber(int min, int max) {
sprintf(random_request, request_format, min, max);
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = random_org;
sin.sin_port = htons(80);
connect(sock, (sockaddr*)&sin, sizeof(sockaddr_in));
send(sock, random_request, strlen(random_request), 0);
char response[1024];
recv(sock, response, 1024, 0);
closesocket(sock);
int random_number;
sscanf(strstr(response, "\r\n\r\n") + 2, "%i", &random_number);
return random_number;
}
void DoneRNG() {
WSACleanup();
}


...sorry

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.