Sunday, May 10, 2009

Write a C++ program that generates 7 random numbers indicating the number of cars crossing a bridge each day (

Write a C++ program that generates 7 random numbers indicating the number of cars crossing a bridge each day (1 random number is assigned for each day [Monday-Sunday]), calculates the daily average number of cars crossing the bridge, minimum and maximum number of cars crossing the bridge in a day and sort the number of cars in descending order. The program should display a menu with the following options.


1.Generate 7 Random Numbers %26amp; Store in an Array


2.Calculate Average Number of Cars Crossing the Bridge


3.Calculate Minimum and Maximum Number of Cars in a Day


4.Sort the Number of Cars Crossing the Bridge in Descending Order


5.Exit


Ex:Assume that the menu option a) generated the following numbers.


33 10 97 89 20 1 67


Option d) should display


97 89 67 33 20 10 1


Wed Thurs Sun Mon Fri Tue Sat


The whole program should work in a loop to enable the user to generate numbers, calculate average, minimum and maximum number oof cars in a day, and sort the numbers untilthe user chooses Exit

Write a C++ program that generates 7 random numbers indicating the number of cars crossing a bridge each day (
Here u go mate...I hope I deserve 10 pts. :)





/*


Write a C++ program that generates 7 random numbers indicating the number of cars


crossing a bridge each day (1 random number is assigned for each day [Monday-Sunday]),


calculates the daily average number of cars crossing the bridge, minimum and


maximum number of cars crossing the bridge in a day and sort the number of cars


in descending order. The program should display a menu with the following options.


1.Generate 7 Random Numbers %26amp; Store in an Array


2.Calculate Average Number of Cars Crossing the Bridge


3.Calculate Minimum and Maximum Number of Cars in a Day


4.Sort the Number of Cars Crossing the Bridge in Descending Order


5.Exit


Ex:Assume that the menu option a) generated the following numbers.


33 10 97 89 20 1 67


Option d) should display


97 89 67 33 20 10 1


Wed Thurs Sun Mon Fri Tue Sat


The whole program should work in a loop to enable the user to generate numbers,


calculate average, minimum and maximum number oof cars in a day, and sort the


numbers untilthe user chooses Exit


*/





#include "stdafx.h"


#include %26lt;time.h%26gt;


#include %26lt;iostream%26gt;





using namespace std;





#define MAX_DAYS7





void usage(){


printf("\n\n1.Generate 7 Random Numbers %26amp; Store in an Array.\n");


printf("2.Calculate Average Number of Cars Crossing the Bridge.\n");


printf("3.Calculate Minimum and Maximum Number of Cars in a Day.\n");


printf("4.Sort the Number of Cars Crossing the Bridge in Descending Order.\n");


printf("5.Exit.\n\n");


}





typedef struct __cars{


int m_iCarCount;


char m_cDays[10];


}CARS;





class CCarCrossing{


CARS m_cars[MAX_DAYS];


CARS m_carsSorted[MAX_DAYS];





public:


CCarCrossing(){


for(int i = 0; i %26lt; MAX_DAYS; i++)


m_cars[i].m_iCarCount = 0;





strcpy(m_cars[0].m_cDays, "MON");


strcpy(m_cars[1].m_cDays, "TUE");


strcpy(m_cars[2].m_cDays, "WED");


strcpy(m_cars[3].m_cDays, "THU");


strcpy(m_cars[4].m_cDays, "FRI");


strcpy(m_cars[5].m_cDays, "SAT");


strcpy(m_cars[6].m_cDays, "SUN");


}





void GenerateCarCount(){


srand( (unsigned)time( NULL ) );


for(int i = 0; i %26lt; MAX_DAYS; i++)


m_cars[i].m_iCarCount = rand() % 100;


}





void DisplayCarCount(){


int i = 0;


for(; i %26lt; MAX_DAYS; i++)


printf("%s\t", m_cars[i].m_cDays);





printf("\n");





for(i = 0; i %26lt; MAX_DAYS; i++)


printf("%d\t", m_cars[i].m_iCarCount);





printf("\n");


}





void DisplaySortedCarCount(){


int i = 0;


for(; i %26lt; MAX_DAYS; i++)


printf("%s\t", m_carsSorted[i].m_cDays);





printf("\n");





for(i = 0; i %26lt; MAX_DAYS; i++)


printf("%d\t", m_carsSorted[i].m_iCarCount);





printf("\n");


}





void DisplayAverage(){


int iTotal = 0;


for(int i = 0; i %26lt; MAX_DAYS; i++)


iTotal += m_cars[i].m_iCarCount;





printf("Average Number of Cars Crossing the Bridge = %d\n", iTotal / MAX_DAYS);


}





void DisplayMaxMin(){


int iMax = m_cars[0].m_iCarCount;


int iMin = m_cars[0].m_iCarCount;


char cMaxDay[10];


char cMinDay[10];


strcpy(cMaxDay, m_cars[0].m_cDays);


strcpy(cMinDay, m_cars[0].m_cDays);


int i = 1;


for(; i %26lt; MAX_DAYS; i++)


(m_cars[i].m_iCarCount %26gt; iMax) ? iMax = m_cars[i].m_iCarCount, strcpy(cMaxDay, m_cars[i].m_cDays) : 0;





for(i = 1; i %26lt; MAX_DAYS; i++)


(m_cars[i].m_iCarCount %26lt; iMin) ? iMin = m_cars[i].m_iCarCount, strcpy(cMinDay, m_cars[i].m_cDays) : 0;





printf("Max Number of cars were on %s. The number of cars were %d.\n", cMaxDay, iMax);


printf("Min Number of cars were on %s. The number of cars were %d.\n", cMinDay, iMin);


}





void Sort(){


for(int k = 0; k %26lt; MAX_DAYS; k++)


m_carsSorted[k] = m_cars[k];





// using insertion sort


int i, j;





for(j = 1; j %26lt; MAX_DAYS; j++){


CARS key = m_carsSorted[j];


i = j - 1;





for(; (i %26gt;= 0) %26amp;%26amp; (m_carsSorted[i].m_iCarCount %26lt; key.m_iCarCount); i--)


m_carsSorted[i + 1] = m_carsSorted[i];





m_carsSorted[i + 1] = key;


}


}





CARS maxCar(int low){


CARS max = m_cars[low];


for(int i = low;i %26lt; MAX_DAYS; i++)


if(max.m_iCarCount %26lt; m_cars[i].m_iCarCount)


max = m_cars[i];





return max;


}





~CCarCrossing(){}


};





int main(int argc, char* argv[]){





int iInput = 0;





//for(int i = 0; i %26lt; 10; i++){


//int j = rand() % 100;


//printf("%d\n", j);


//}


CCarCrossing cars;


for(; iInput != 5;){


usage();


scanf("%d", %26amp;iInput);


switch(iInput){


case 1:


cars.GenerateCarCount();


cars.DisplayCarCount();


break;


case 2:


cars.DisplayAverage();


break;


case 3:


cars.DisplayMaxMin();


break;


case 4:


cars.Sort();


cars.DisplaySortedCarCount();


break;


case 5:


exit(0);


break;


default:


printf("opt NULL\n");


}


}





return 0;


}
Reply:There's not enough space here to write the whole thing out, but I can give you some pointers.





Fill an array with random numbers:





// Need this for the rand() function


#include %26lt;stdlib.h%26gt;





const int cNumValuesToMake=7; // 7 days in a week


const int cMaxNumber=100; // 1-100 cars per day





// Array to store the values


int myArray[cNumValuesToMake];





for (int i=0; i%26lt;cNumValuesToMake; i++)


{


myArray[i]=(rand()%cMaxNumber)+1;


}





You can sort that array using a bubble sort, as so:





for (int i=cNumValuesToMake-1; i%26gt;0; i--)


{


for (int j=0; j%26lt;i; j++)


{


if (myArray[j]%26lt;myArray[j+1])


{


int temp=myArray[j];


myArray[j]=myArray[j+1];


myArray[j+1]=temp;


}


}


}





And finally figure out the mean, min and max as so:





int min=cMaxNumber;


int max=0;


float mean;





for (int i=0; i%26lt;cNumValuesToMake; i++)


{


if (myArray[i]%26gt;max)


max=myArray[i];


if (myArray[i]%26lt;min)


min=myArray[i];


mean+=(float)myArray[i];


}





mean/=(float)cNumValuesToMake;





Hope this helps :)

flower show

What wiil be code for generating a random number in c++?

#include %26lt;iostream%26gt;


#include %26lt;cstdlib%26gt;


#include %26lt;time.h%26gt;


int main()


{


/*


Declare variable to hold seconds on clock.


*/


time_t seconds;


/*


Get value from system clock and


place in seconds variable.


*/


time(%26amp;seconds);


/*


Convert seconds to a unsigned


integer.


*/


srand((unsigned int) seconds);


/*


Output random values.


*/


cout%26lt;%26lt; rand() %26lt;%26lt; endl;


cout%26lt;%26lt; rand() %26lt;%26lt; endl;


cout%26lt;%26lt; rand() %26lt;%26lt; endl;


return 0;


}

What wiil be code for generating a random number in c++?
there is a 'rand' function to generate it. See help.


How do i generate 16 bit random number in c?

You don't mention which compiler or operating system, but the ISO/POSIX standard function is rand() - there is a good write-up here: http://www.delorie.com/gnu/docs/glibc/li...





The return value is typically a 32 bit integer, but this can vary depending on the C library. You should be relatively safe casting a 32 bit return value into a 16 bit variable but, depending on your compiler, beware of sign extension issues.





A simple example:





short int my_rand;





srand (time(0));


my_rand = (short int)rand();





Note that rand() is really only a pseudo-random number generator, but it should be good enough for your purposes.





If you're on a Unix-like system, you have other options with the BSD and SystemV random number variants (referenced in the article above) but rand() is the most portable.

How do i generate 16 bit random number in c?
right click on your desk top click on properties go to settings and you can change it from there


How to get a diff random number in C?

It seems to be explaned in this link.


If you want a different random number then call the random function a second or third time.

How to get a diff random number in C?
just if u know little math , try to know an good equation for generate random numbers then use it and assign the first number





thanks,


Random number generator in c++?

I created a number guessing game using c++. But for some reason it always generates the same numbers every run time.


How can this be fixed.

Random number generator in c++?
you should call the function srand() each time before rand() to create a different sequence of numbers.





you should include cstdlib.h header file


#include %26lt;cstdlib%26gt;


using std::srand;





srand() receives an integer as argument, for each integer passed to it, rand() will generate a different sequence.





you can pass the time of system to srand() like this:


srand( time(0) );


and of course you will have to include time.h header file in your source code.
Reply:this all programing, not too good with programming





sorry
Reply:You must "seed" the random number generator in order to get different random numbers out of rand(). The seed should be different for each program execution, and an easy way to do this, is to seed it with the time. Note that if you execute this program twice in the same second (unlikely), you will get the same value as the seed will not change.





Example:


#include%26lt;cmath%26gt;


#include%26lt;iostream%26gt;


#include%26lt;ctime%26gt;





int main()


{


srand(time(0));


std::cout %26lt;%26lt; rand() %26lt;%26lt; std::endl;


return 0;


}

phone cards

Can someone help me how to code a random number in Turbo C?, like a random selection in numbers 1,2 and 3.?

cause im doing a game in Turbo C which is rocks-papers-scissors but it's against a computer player, that's why i need to make it random for the computer's choice, for example 1=paper, 2=scissors, and 3=rocks.

Can someone help me how to code a random number in Turbo C?, like a random selection in numbers 1,2 and 3.?
Since there are 3 elements in the array, need to generate a random number between 0 and 2. Let me note, I haven't done this in Turbo C, but I have done it in Dev-C++, both in C++ and normal C.





If doing it in C, you will need the header file: time.h


So it's be: #include %26lt;time.h%26gt;





In C++, you don't need to use that header file. Using the standard namespace will do fine.





From here on, generating the number is same in both languages.





First, you need to know something. Computers can't generate TRULY random numbers. It uses a long algorithm to create a number which seems random. But if you run the application again, it'll generate the same random numbers all over again. To get around this, we need to "plant" a variable into the generation algorithm, calle "seeding". Most common thing to seed it with is the System Time.





Like so: srand(time(NULL));





Remember, you need to have that ^ statement BEFORE you begin generating numbers. This should make them more random.





From there, you need to generate random numbers. When you generate a random number, it'll be between 0 and 32767 (depends on the system I guess). To get it down to within a small range from 0 to n, we need to do some modular division.





Modular division (in case you don't know), is dividing a number and returning the remainder. Since we want the number to be between 0 and 2, we need to do:





{RANDOM NUMBER} % 3. This will always result in a 0, 1, or 2.





From here, it's easy. To generate a random number (between 0 and 32676) use RAND().





int randomNumber = rand() % 3;





That ^ statement above will generate a number between 0 and 2, which can then be used directly as an index for your array.





Hope my explanations were adequate.
Reply:There are functions for generating random numbers like rand();





Observe the following code and see how random numbers are generated





/* rand example: guess the number */


#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;time.h%26gt;





int main ()


{


int iSecret, iGuess;





/* initialize random seed: */


srand ( time(NULL) );





/* generate secret number: */


iSecret = rand() % 10 + 1;





do {


printf ("Guess the number (1 to 10): ");


scanf ("%d",%26amp;iGuess);


if (iSecret%26lt;iGuess) puts ("The secret number is lower");


else if (iSecret%26gt;iGuess) puts ("The secret number is higher");


} while (iSecret!=iGuess);





puts ("Congratulations!");


return 0;


}


Help! Non- repeating random number generator (C programming)?

I'm trying to generate non-repeating random numbers(within 1-16) in 4x4 matrix, but some numbers among 16 keep repeat. I also want to make the number 16 to be left with a blank, so I put:


if(16 == array[x])


{


array[x] == ' ';


}





which does not work either...





Please help me out!!








#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;time.h%26gt;


int main()


{


int array[16] = {0};


int x;


int y;


int m;


int n;


srand(time(0));


for(x = 1; x %26lt; 17; x++)


{


array[x] = rand()%17;


}


for(x = 1; x %26lt; 17; x++)


{


for(y = 0; y %26lt; 17; y++)


{


if( x != y)


{


if(array[x] == array[y])


{


array[x] = rand()%17;


if(16 == array[x])


{


array[x] == ' ';


}


}


}


}


printf("%5d", array[x]);


if(x%4 ==0)


printf("\n");


system("PAUSE");


return 0;


}

Help! Non- repeating random number generator (C programming)?
So, what you want is to fill an array with 16 numbers, with each number being different, and the number 16 is replaced with a space character, right?


I'll just concentrate on the random number generation and array filling part of your program.





To debug your program, you should simplify it first. For now, ditch the nested loops and printing part, and create a test program which just includes the random number generation and array filling part.





You need an algorithm which works like this:





First, allocate the array in global memory by putting the 'int array[16];' line outside of the 'main()' procedure. (The C start up code automatically zeroes out all global memory variables when you run the program.)





Now do this:


1) Create random number and put it in an int variable.


2) Is the int a value of 16?


3) If so, then change the value to the ' ' character. The ASCII value of ' ' is 32 decimal.


4) In a 'while' loop, compare the int to the other ints in the array. The loop ends if you read a zero in the array.


5) If the int matches a value in the array, go back to step 1.


6) Else, put the int in the array. You need a variable to keep track of which array element to write to.


7) If you haven't written out 16 ints to the array, go back to step 1.





As you can see, the algorithm above requires 2 'while' loops -one inside the other. The inner loop compares the random number to the numbers already in the array. The outer loop runs until the array is filled with 16 ints.





Printing the array is another matter.


Non repeating random number generator ( C programming)?

I've been working on non repeating random number generator... and it doesnt work.. First, it doesnt generate "random" numbers every time I run it.. and second, some numbers repeat several times.





int main()


{


int array[16] = {0};


int x;


int y;





for(x = 1; x %26lt; 17; x++)


{


array[x] = rand()%16+1;





for(y =1; y %26lt; 17; y++)


{


if(array[y] == array[x])


{


if(y != x)


{


rand()%16 +1 == array[x];


}


}


}


if(array[x] == 16)


array[x] == ' ';


printf("%3d", array[x]);





if(x%3 ==0)


printf("\n");


}


system("PAUSE");


return 0;


}





Can someone tell me what is wrong with my program? Thanks!

Non repeating random number generator ( C programming)?
According to the documentation: The rand function returns a pseudorandom integer in the range 0 to RAND_MAX. Use the srand function to seed the pseudorandom-number generator before calling rand.





Also please note when declaring the following:


int array[16] = {0};


you are making space for 16 int elements ie 0-15 so writing to array[16] is a problem, you may only write from array[0]-array[15]. Ive rewritten your for loops accordingly.








#include %26lt;time.h%26gt; // we are using time()





int main()


{


int array[16] = {0};


int x;


int y;





/* Seed the random-number generator with current time so that


* the numbers will be different every time we run.


*/


srand( (unsigned)time(NULL) );





for (x = 0; x %26lt; 16; x++)


{


array[x] = rand()%16+1;





/*


No need for this as you are already


filling the array correctly now





for(y=0; y %26lt; 16; y++)


{


if(array[y] == array[x])


{


if(y != x)


{


rand()%16 +1 == array[x];


}


}


}


*/


// This is wrong, the space character ' ', shouldnt be used


// in an int array and you are replacing any part of the


// array that equals 16 which isnt correct.


//if(array[x] == 16)


// array[x] == ' ';





printf("%3d", array[x]);





// I added x+1 so that the x=0 value is printed in the


// first row of 3


if((x+1)%3 == 0)


printf("\n");


}





system("PAUSE");


return 0;


}
Reply:i dont understand c yet, still in high school im getting there :), but are you resetting the for loop after a test fails?





for example if array(blah blah blah) = array(blah blah blah)





x = 1
Reply:Define an array of 16 elements with the numbers 1 to 16.


In a for loop of 8 times


get a random number between 0 to 15 // array subscript


do while (array[rnd]=0)


get a random number between 0 to 15 // array subscript


end do


Print array[rnd]


array[rnd]=0


end for





for i 0 to %26lt;16


if array[i] %26gt; 0


print array[i]


end-if


end for





If you make the first loop do all the work and do not use the second loop then the program will be quite slow getting the last few numbers.


by having the second loop the program will be very quick. You can play about with the number of times round the first loop.





Have fun.
Reply:dunno about c, but with VB you need to say "randomize" otherwise every time you run it it'll be the same





just my two cents
Reply:OK, first off, you want to seed the random number generator with a call like so:





srand( (unsigned)time( NULL ) ); //crucial!





2nd, I like to use a macro for making random numbers...i forget who gave me this, but thanx, you!





#define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))





//...you might use it like so:


BOOL roll_dice_is_snake_eyes()


{


int d1,d2;





d1=GetRandom( 1, 6 );


d2=GetRandom( 1, 6 );


if(d1 == 1 %26amp;%26amp; d2 == 1)


return 1; //dice are snake eyes!


return 0; // not....


}





OK, 3rd, please know some of the science on this: Namely, just because some numbers repeat several times...that doesn't mean the code is failing.





For example, By definition, for 100 tries of random integers in the range of 1 thru 10, you *should* see each integer repeated 10 times (an even distribution) ...if the generator is _truly_ random.





If your generator must not repeat, then each time a new number is generated, store it in an array. Parse the array to ensure it wasn't already used before handing it to the user.








int array[20];





int used_already(int num)


{


int ii=20;


while(ii--)


if(array[ii] == num)


return 1;


return 0;


}





int store_it(int num, int count)


{


array[count++]=num;


return count;


}





int main()


{


int num, count = 0;





while(count %26lt; 20)


{


num =GetRandom(1,19);


if( ! used_already( num) )


{


count = store_it ( num, count );


printf("Hey! Good one: %d\n", num);


}


}


}





cheers!


Trouble making a function in Visual Studios for C++ for a random number generator?

the following is what i have running in visual studios right now for C++. i am taking a computer science class and we need to make a function that will return a randomly generated number. i managed to create the random number, but once i put it in the form of a function it won't work. i get an error message that says: " 'getrand': must return a value "





please help me figure out what im doing wrong here. and i can not redo this in another fashion, she only wants us using what we've learned so far, and she wants the function to be a double function that returns a number between 0 and 1








#include %26lt;iostream%26gt;


#include %26lt;ctime%26gt;


#include %26lt;cstdlib%26gt;


using namespace std;


double getrand();


int main ()


{


getrand();


return 0;


}


double getrand()


{


int ctr=0;


srand ((unsigned) time(NULL));


while (ctr%26lt;1)


{


cout %26lt;%26lt; (float)rand()/RAND_MAX %26lt;%26lt; endl;


ctr ++;


}


}

Trouble making a function in Visual Studios for C++ for a random number generator?
Instead of printing the random number to standard output, return it:





return (double)rand()/RAND_MAX;





then in main:





cout %26lt;%26lt; getrand() %26lt;%26lt; endl;
Reply:"she wants the function to be a double function that returns a number between 0 and 1"!





Well, you're half way there. You declared your function as a double. Now make it return a number between 0 and 1!

orange

Please interpret c++ code; random number generator?

I am trying to generate a random number specifically in the range (-1, 1). I want to understand what the difference between the two lines below is; the first one gives me what I am looking for but i don't quite understand how it works. the second line does not work at all and I was wondering if anyone could suggest any alternatives. Thank you.





1. u = (2.0*rand()/RAND_MAX) - 1;


2. u = rand() / RAND_MAX ;

Please interpret c++ code; random number generator?
1. u = (2.0*rand()/RAND_MAX) - 1;





rand()/RAND_MAX





This gives you a number between [0,1]


Then you multiply by 2 and subtract 1.





You see:


2 * 1 = 2 , 2 -1 = 1 your max


2 * 0 = 0 , 0 -1 = -1 your min





2. u = rand() / RAND_MAX ;


This one only gives you values from 0 to 1
Reply:Some compilers could do rand()%3 - 2 but it's not guaranteed safe.





The following URL goes into a bit more detail: http://www.daniweb.com/forums/thread1769...
Reply:Hi,





Lets first look at the definition of the function rand():


int rand ( void );


Returns a pseudo-random integer number A in the range 0 to RAND_MAX or formally in the interval [0...RAND_MAX], A iselementof [0...RAND_MAX].





Lets first start with the second line of code, as this is the simplest one. The line u = rand() / RAND_MAX actually scales the generated number A=rand() so that it lies in the interval [0...1]. This can be seen as follows:


If A is the smallest number that can be generated by rand(), that is 0 then you have 0 / RAND_MAX = 0.


Likewise, if A is the largest number that can be generated by rand(), that is RAND_MAX then you have RAND_MAX / RAND_MAX = 1. In other words u iselementof [0/RAND_MAX...RAND_MAX/RAND_MAX] = [0...1].





The second line of code:


u = (2.0*rand()/RAND_MAX) - 1;





rand()/RAND_MAX generates a random number in the interval [0...1].





2*rand()/RAND_MAX generates a random number in the interval 2*[0...1] = [0...2].





2.0*rand()/RAND_MAX) - 1 generates a random number in the interval 2*[0...1] - 1 = [0...2] - 1 = [-1...1].
Reply:I think to answer your question then you have to understand what rand() is doing. The rand() function is returning a value between 0 and 1. This value can be multiplied against another value to give a desired random value within a range between 0 and that number. Why this works is beecause the decimal number you get out of rand(), when multiplied against a nubmer, is giving you a percentage of that number.





Remember that 100% is equal to 1 in decimal form. 50% is equal to .5 in decimal form. So if you want a random number between 0 and 100 and you multiple this percentage against it you will get a random number in that range. 100 * 1 = 100 ... 100 * .5 = 50





So with that in mind, now you should be able to understand what 2 * rand() is doing ... it gives you a random number between 2 and 0.





RAND_MAX is equal to 1, so that is doing nothing for you at all. You can forget about the divide by RAND_MAX. A number divided by 1 is itself.





The -1 is the key to getting the value in the range you want. if you take a random number between 0 and 2 and subtract 1 then you now have a random number between -1 and 1. (0 - 1 = -1 ... 2 - 1 = 1)





So really, the only thing you need is just the 2.0 * rand() - 1;


How to generate random number in C?

How do I generate a random number in C programming such that the random seed no. srand() has a value that changes each time the program is run? Can any one type a simple function that generates a random number between 0-9?





Also, what can I use to compile the programme? And how do I run it?





Thanx.

How to generate random number in C?
Modifying the example in the Visual C++ help to adjust to your range:





#include %26lt;stdlib.h%26gt;


#include %26lt;stdio.h%26gt;


#include %26lt;time.h%26gt;





void main( void )


{


int i;





/* Seed the random-number generator with current time so that


* the numbers will be different every time we run.


*/


srand( (unsigned)time( NULL ) );





/* Display 10 numbers. */


for( i = 0; i %26lt; 10;i++ )


printf( " %d\n", 10 * rand() / (RAND_MAX + 1) );


}
Reply:if you want to get numbers between 0 and 9, MOD the number by 10, any number mod 10 is between 0 and 9 by definition, so


int random;


random = rand() % 10;





will set random to a number between 0 and 9, remember you're talking about the REMAINDER after dividing by 10.An equal number of random numbers will have remainders of 0,1,2,3...or 9.
Reply:You'll need to include the math library:





include %26lt;math.h%26gt;





Then just use:





x = rand(4,77); // random number between 4 and 77.





done!


Random number c# wont generate same number 2s?

basicly im making a simple program that genrates numbers for a game 1 - 10 but it cant generate same number 2s its to decide which player operates which control





int sd;


Random rnd =new Random();


sd= rnd.Next(10);





is simple code for random number how ever i did then think to put all 55 numbers in a list box and use index to genrat random index number every time get that value then remove it from the index how ever this didnt work as well as id hope so id like to know if any 1 knows another way or if they can think of a betta way of doing it thro list box

Random number c# wont generate same number 2s?
Pull all 55 numbers in a list box and use index to generate random index numbeer everytime get that value then set the value to something so you can do a conditional statement so if that number gets pulled it performs the task again and again till it gets a number that isn't used yet.





NOTE: The above phrase is based on some of what you have said.. I will explain it below as I would understand it.





1) Create an array with your values as the elements.


2) Randomly select one of those elements.


3) Set that elements value to NULL


4) Randomly select one of those elements


5) Make sure the element isn't null, If null don't accept and try again until you get it right


6) Set that elements value to NULL


7) Continue until all numbers selected.