Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to get random value

27 views
Skip to first unread message

arief...@gmail.com

unread,
Feb 25, 2019, 2:32:56 AM2/25/19
to
hi guys
when i run my program, result stack in 37, i want result of this script as below will be random, how to get the best script
#include <iostream>
//using namespace std
#include <stdio.h>
#include <stdlib.h>

int main() {
int c, a;
int n;
a = rand()% 50 +1;
printf("value a is %d \n",a);

for(c =1; c <= a; c++) {
n = rand()% 50 + 1;
//n = rand();
printf("value : %d \n",n);
return n;
}
printf("value of n : %d \n",n);
if (n > 50){
printf(" lowest : %d",n);
}
else printf("highest : %d",n);
return 0;
}

Alf P. Steinbach

unread,
Feb 25, 2019, 3:26:25 AM2/25/19
to
On 25.02.2019 08:32, arief...@gmail.com wrote:
> hi guys
> when i run my program, result stack in 37, i want result of this
> script as below will be random, how to get the best script
[Reformatted with AStyle:]
> #include <iostream>
> //using namespace std
> #include <stdio.h>
> #include <stdlib.h>
>
> int main() {
> int c, a;
> int n;
> a = rand()% 50 +1;
> printf("value a is %d \n",a);
>
> for(c =1; c <= a; c++) {
> n = rand()% 50 + 1;
> //n = rand();
> printf("value : %d \n",n);
> return n;
> }
> printf("value of n : %d \n",n);
> if (n > 50) {
> printf(" lowest : %d",n);
> }
> else printf("highest : %d",n);
> return 0;
> }

If you want different pseudo-random sequences then you have to provide a
/seed/ value that's different in each run of the program.

For the machinery you're using you pass that seed value to `srand`, but
where to get a varying seed?

One portable possibility is to use `time`, but note that it doesn't vary
fast enough to guarantee different values in close runs of the program.

Another portable possibility is to use the modern C++ random generation
facilities just for this, `std::random_device`. Declare such an object.
It's callable, so you just call it (like a function) to get the random
seed value. Note: at least as version 8.2.0 g++ wasn't quite up to date
in its implementation; if it doesn't work with g++ you can define
`_GLIBCXX_USE_RANDOM_TR1` globally in the build.

Of course, using the modern facilities for a random seed, you might
almost just as well use them also for the random number generation. It's
slightly more complex code, though. Unless you write a reusable wrapper.


Cheers!,

- Alf

James Kuyper

unread,
Feb 25, 2019, 11:36:50 AM2/25/19
to
You've already been told that the solution is to call srand() with a
suitably chosen value, along with some hints as to how to choose such a
value. However, it wasn't explained why that was necessary. Here's the
key reason:

"If rand is called before any calls to srand have been made, the same
sequence shall be generated as when srand is first called with a seed
value of 1." (7.22.2.2p2).

Ike Naar

unread,
Feb 28, 2019, 6:05:06 PM2/28/19
to
On 2019-02-25, arief...@gmail.com <arief...@gmail.com> wrote:
> for(c =1; c <= a; c++) {
> n = rand()% 50 + 1;
> //n = rand();
> printf("value : %d \n",n);
> return n;
> }

What's the use of having a for loop when it always returns
during the first iteration?
0 new messages