The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how can i isolate the random sequence of numbers using awk? rcon1 UNIX for Dummies Questions & Answers 2 01-09-2008 09:06 AM
generating random numbers with hamming distance 4 hack_tom Shell Programming and Scripting 2 04-29-2007 01:27 AM
genrating pseudo random numbers hack_tom Shell Programming and Scripting 7 04-27-2007 10:10 PM
Random numbers without repetition asal_email UNIX for Dummies Questions & Answers 8 07-14-2005 04:02 AM
Sed - Replacing numbers w/asteriks in random location in a file giannicello UNIX for Dummies Questions & Answers 9 10-04-2001 12:03 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-11-2008
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
How to set constrain on random numbers in c

Hi,

I am currently trying to generate multiple random numbers in C for different variable:-


die1=1+(rand()%5);
die2=1+(rand()%5);
die3=1+(rand()%5);
die4=1+(rand()%5);

But I need to contrain the total of die1, die2,die3 and die4 to be 5 as well. If i insert die1+die2+die3+die4=5, i do not think it will constrain the distribution.


Please advise. Thanks.


-Jason
  #2 (permalink)  
Old 01-11-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Your four variables are integers, each set to 1 + some integer. Three of them must stay at 1 and the fourth one can be two. Pick a number between 1 and 4 and increment only one variable based on that. You pick a 3, die3=2 and the rest stay at 1.
  #3 (permalink)  
Old 01-11-2008
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
Yeah. I understood the concept. But do we have ways to automate them to generate all sorts of combination in this constrain?

I tried to find from internet related to these cases; they arent related. Most of them are just giving random numbers generation.

As I mentioned previously, if i constrain the total value after the random number is generated for these variables, it wont work because it is not dependent. Unless I can slot something in between the rand?

Thanks.

-Jason
  #4 (permalink)  
Old 01-11-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
If you had, say, 100, to distribute 5 into 5 variables: You pick a number from 1 to 100, let's say you get 20. The first variable gets the 20. Now 100 - 20 = 80 is left. So pick a number between 1 and 80. You stop when you get to only 1 variable and it gets the remainder. Or if the amount left reaches zero, you stop early and the rest of the variable must be zero.
  #5 (permalink)  
Old 01-11-2008
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
Hi,

I get the idea now. Thanks. But i just have a thought if that is the case, would it be very bias to have the top variables always get the most of the values rather than the bottom ones?

What I expected at the first place is to have fair distribution of cake among the variables.

let say i have only consider 3 possbile values, (0,1 and 2). And have 5 variables. If I did consider the below idea, in all the cases, the second of third variables always get the most rather than the fourth and fifth variable.

Please advise. Thanks..

Rgrds,
Jason
  #6 (permalink)  
Old 01-11-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
I can't help you there since your test case is so very constrained. Except to say that only a very few possible outcomes are going to satify you. Enumerate them and pick one.

Getting back to my test case, you could preallocate 10 to each variable and then distibute the remaining 50 as I indicated. Or you could distribute the numbers one at a time. Cycle though each variable and generate a 0 or 1. Add that to each variable and subtract it from the mass to be distributed.
  #7 (permalink)  
Old 01-11-2008
vikashtulsiyan vikashtulsiyan is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 42
Quote:
Originally Posted by Perderabo View Post
If you had, say, 100, to distribute 5 into 5 variables: You pick a number from 1 to 100, let's say you get 20. The first variable gets the 20. Now 100 - 20 = 80 is left. So pick a number between 1 and 80. You stop when you get to only 1 variable and it gets the remainder. Or if the amount left reaches zero, you stop early and the rest of the variable must be zero.
jason Perderabo idea is cool. but if u want ot " distribute piece of cake evenly" then you can write an additional code

n=1+rand()%4
switch(n)
{
case 1:
// use perderabo idea starting with die1 - die4

case 2:

// use perderabo idea starting with die2 - die4,die1 in circular way

case 3:
//and so on...........
}

i know its not a very pretty solution but considering your unique constraints this is the best solution i got
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0