Question:
Program in C++ to generate random values within a given range?
rukh
2010-04-18 07:59:09 UTC
I am having problems writing source code for this, can anybody help me
Write A Program in C++ To Generate Random Values From 1 To 5 Without Repetition. then assign the randomly generated values to 5 Strings Of An Array ("Anna", "Barbara","Charles","David","Eliza")
Four answers:
JAEGER
2010-04-22 07:29:18 UTC
include

#include

#include



int main ()

{

srand(time(0));



cout << "Random number between 1 to 5: " << rand() % 5 + 1<


return 0;

}















To make sure not to have any repeated numbers, use a list for example to save the generated value, then test if the value does exists in the list before using it ;)
lenette
2016-12-03 07:02:34 UTC
define an array of sixteen factors with the numbers a million to sixteen. In a for loop of 8 situations get a random type between 0 to fifteen // array subscript do on a similar time as (array[rnd]=0) get a random type between 0 to fifteen // array subscript end do Print array[rnd] array[rnd]=0 end for for i 0 to 0 print array[i] end-if end for once you're making the 1st loop do each and each and each and all of the paintings and don't use the 2nd loop then this technique would be quite sluggish getting the quite suitable few numbers. by making use of making use of having the 2nd loop this technique would be very rapid. you may play approximately with this form of situations around the 1st loop. have relaxing.
tbshmkr
2010-04-18 08:42:34 UTC
Random Number Code

=

#include

#include

#include

using namespace std;



int main ()

{

srand(time(0));

cout << "Random number: " << rand() % 5 << endl; //Random 0 to 5



return 0;

}
cb
2010-04-19 18:03:13 UTC
How do you then assign the strings of an array?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...