473,890 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

random numbers with threads

Hi, I wrote a program with two threads playing a number guessing game.
one thread pick a number the other try to guess it. Also first thread
say try a lower or higher number. but my program crashes in defining
the bounds for generating random numbers.
For instance the first thread pick 35
the other say 19. he should pick a number bigger than 19. for now
that's ok. now second thread say 60. then first thread say lower
number. but my second thread is not like that. how i can define that
borders?

thanks a lot

Oct 17 '06 #1
3 1650
hande wrote:
but my program crashes in defining
the bounds for generating random numbers.
What program?
What about posting some code?
Oct 17 '06 #2

Martin Steen wrote:
hande wrote:
but my program crashes in defining
the bounds for generating random numbers.

What program?
What about posting some code?

else if(num<gnum)
{
gnum=rand()%gnu m;
temp2=gnum;
cout<<"Thread2: "<<gnum<<en dl;
// pthread_mutex_u nlock(&myMutex) ;
}
else if(num>gnum)
{
gnum=rand()%(10 0-gnum);
cout<<"Thread2: "<<gnum<<en dl;
//pthread_mutex_u nlock(&myMutex) ;
}

Oct 17 '06 #3
hande" <hc*****@gmail. comwrote in message
news:11******** **************@ e3g2000cwe.goog legroups.com...
Hi, I wrote a program with two threads playing a number guessing game.
one thread pick a number the other try to guess it. Also first thread
say try a lower or higher number. but my program crashes in defining
the bounds for generating random numbers.
For instance the first thread pick 35
the other say 19. he should pick a number bigger than 19. for now
that's ok. now second thread say 60. then first thread say lower
number. but my second thread is not like that. how i can define that
borders?

thanks a lot
First, try your program without the threads. See if it works that way. If
it does, then it's a threading issue and post some code at:
comp.programmin g.threads that is small enough to show the problem.

If it does not without the threads, it's a C++ error, post the smallest code
here that compiles that wll show the error.
Oct 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6664
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause potential problems for thread-safety. So far, I'm only confused. I need a proper explanation for the concept so I can understand how to write thread-safe functions in the future. My apologies for posting a long routine.
21
3043
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to generate a set of random number that follow a normal distribution N(0,1) ? I am develloping on power4 machine running AIX. Thank you for your help
5
2220
by: user | last post by:
Hello i call it in almost same time (from two different threads): Random rand = new Random(); id=rand.Next(65535); and i often receive the same results. Is it possible ? How windows count this random ? Thanx
11
1849
by: Mark Rae | last post by:
Hi, My R&D department has asked me to look at threading in a Web Service written in C#, so I came up with the following code: using System; using System.ComponentModel; using System.Threading; using System.Web.Services;
12
5237
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's RAND(). any ideas? I suppose I could use the current rancom number as the seed for the next random number. but would that really work?
6
4000
by: D. Patrick | last post by:
I have a multi-threaded app that needs to generate random 8 digit numbers. I'm using VB.NET 2.0. The problem is that >1 request for a random number can occur in the same millisecond. I can't seed with a time value when the time for 2 (or more) threads is identical. Even ticks is identical. So how can I generate a random number that will not likely be the same as the number returned by another thread running at that same millisecond? ...
2
3669
by: HumanJHawkins | last post by:
I wrote this question a little differently in the MFC group since it is a bit of a different environment than pure C++, but I hope you will forgive the similarities if anyone is reading both groups. Basically, I need to seed the random number generator in 4 seperate threads at once. So, I can't use the current time as the random seed, because then all four threads end up with the same series of (therefore non-random) numbers. Is there...
41
2393
by: Zytan | last post by:
Anyone do any tests on it? I would assume it has improved since C's rand(), but who knows. For some reason, and it could just be coincidence, I seem to see patterns. But even a crappy rand() function should be good enough to avoid that, so i may be seeing things. Zytan
13
2822
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000, or between 0 and 99999 (inclusive). Further, the I want the range to be a variable. Concretely, I would like to create the following method: unsigned long Random( unsigned long num )
0
9976
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9815
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8008
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4665
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4259
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.