Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 06:58

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



C++ problem random

Discussion on C++ problem random within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Dec 2011
Posts: 367
Received Thanks: 199
C++ problem random

Hi community,
i have a small problem with the random function in the thread!

Code:
int rand_func()
{
    srand((unsigned int)time(NULL));
    int output = rand()%3;
    return output;
}

void test(void *id)
{
   int n = rand_func();
}
i opened two threads with _beginthread() but n of first thread is equal to n of second thread ..

help me please, i need a random number from 0 to 3..
the second n should not be equal to the first n!

if possible I would like to know the reason

sorry for my english *-*
Kingrap is offline  
Old 02/13/2013, 18:48   #2
 
Schlüsselbein's Avatar
 
elite*gold: 0
Join Date: Feb 2013
Posts: 1,137
Received Thanks: 869
You should call srand only once (for instance in your main routine).
Schlüsselbein is offline  
Old 02/13/2013, 18:52   #3
 
Raz9r's Avatar
 
elite*gold: 297
Join Date: Dec 2010
Posts: 1,129
Received Thanks: 1,687
Quote:
Originally Posted by Schlüsselbein View Post
You should call srand only once (for instance in your main-routine).
Also, this is rather C than C++. Note that there exists no language named "C/C++" for a reason.
What about the headers <random> and, not to forget, <thread>?
Raz9r is offline  
Old 02/13/2013, 20:41   #4


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,902
Received Thanks: 25,407
Btw. did you try the code more than once? That range is rather small, it could be just an accident that both threads got the same result.
MrSm!th is offline  
Old 02/13/2013, 22:58   #5
 
elite*gold: 0
Join Date: Dec 2011
Posts: 367
Received Thanks: 199
i have solved the problem thank
Kingrap is offline  
Old 02/13/2013, 23:38   #6
 
Schlüsselbein's Avatar
 
elite*gold: 0
Join Date: Feb 2013
Posts: 1,137
Received Thanks: 869
Quote:
Btw. did you try the code more than once? That range is rather small, it could be just an accident that both threads got the same result.
I dont think so.
For example:
Code:
#include <iostream>
#include <ctime>
#include <vector>

int main()
{
	const unsigned int length = 500000;

	std::vector<unsigned int> v;
	v.reserve(length);

	for(int i = 0; i < length; ++i)
		v.push_back(static_cast<unsigned int>(::time(NULL)));

	std::cout << "Element 0: \t" << v.front() << std::endl;
	std::cout << "Element " << length << ": \t" << v.back() << std::endl;
}
For me with my rather slow pc (1,9ghz Dual Core processor) i need up to 500k iterations in debug mode before i get t0 + 1 second.
Therefore he seeds the random number generator with the same seed -> same result.
Schlüsselbein is offline  
Old 03/03/2013, 23:58   #7
 
elite*gold: 0
Join Date: Sep 2010
Posts: 17
Received Thanks: 1
you can have some other codes
here :
jomergallano is offline  
Reply


Similar Threads Similar Threads
[SALE] Random ISRO chars in random servers CHEAP, from 1$ to 3$
06/03/2012 - Silkroad Online Trading - 4 Replies
Hello, I want to sell all those chars very cheap for PayPal money from 1$ to 3$ , because I need some money to buy usb joystick to play FIFA on pc ^^ If you buy them all, I can sell all those chars for 15$!!! Contacts - skype: myntex2 I ACCEPT PAYPAL ONLY So let's start:
problem - pointer and (random values)
03/19/2010 - General Coding - 0 Replies
Im trying to learn to reverse engineer a game. Its a game and it has an item to add up random stats to an equip when it is used. First, i tried using a memory scanner to determine which of memory holds the value i want to manipulate. hoping it would be that easy, i was wrong because it was holding the value that the game uses to let the user see. so i guessed it was a pointer. so i found the pointer. one was changing every second (clue) the other was not a pointer it was an address...
EpicWebserver Random Problem
12/11/2009 - CO2 Private Server - 3 Replies
Alright, I got my register page finally working last night and it worked fine for everyone. But today randomly, without me touching anything it throws me this error. I restart it and it doesn't give the error, I register accounts fine with it, and randomly today I got a console error. People can go to the page where it says enter your account info to register but when they register it loads forever and then says " This webpage is not available. The webpage at...
Problem on Retail Random Teleporting
07/06/2009 - Lineage 2 - 4 Replies
Well i am botting a train on retail using og walker 10.9.7.I am botting in catacombs and sometimes i check them and i see one of my characters in fanasty island just sitting there by one of the fanasty islands game managers. This has happened to me three times so far with 3 different characters. It seems they just get ported out of the catacomb to the fanasty island game manager for some reason. I am sure its not a gm cause i would be banned by now. I was wondering if this has happen to...
Sleep(Random($min, $max)) ==> Problem
04/07/2009 - GW Bots - 20 Replies
Hallo :D ich bins mal wieder, ich habe folgendes Problem: Ich habe den Befehl Sleep(Random($min, $max)) in eine Funktion gefasst: Func RS($min, $max) Sleep(Random($min, $max, 1))



All times are GMT +1. The time now is 06:59.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.