0

I'm working on a group project where I need to generate a set of random numbers, however, the values are staying the same. The values should be between 0 and 1 in a float value.

The code is as follows:

srand((unsigned int) time(0));

float randNumber = (float) rand() / (float) (RAND_MAX) * (float) 1;
if (randNumber < 0.05) {
    state = STATE_HEALTHY;
} else if (randNumber > 0.05 && randNumber < 0.15) {
    state = STATE_INFECTED;
} else {
    state = STATE_HEALTHY;
}
printf("[%i][%i] Generated Number: %.2f  |  State: %i\n", X_INDEX, Y_INDEX, randNumber, state);

If I put the rand() in a printf at the end underneath the last printf the number returned is always random.

The end goal for this is the output below should have a randomly generated number for each index (of x and y)

[0][0] Generated Number: 0.20  |  State: 0
[0][1] Generated Number: 0.20  |  State: 0
[1][0] Generated Number: 0.20  |  State: 0
[1][1] Generated Number: 0.20  |  State: 0

How would it be possible to generate a random number for cell?

Edit: After using the suggestion by @drescherjm I am faced with the issue of the first value not being random but instead being incremented very slowly by one, see below for two outputs after the suggestion.

[0][0] Generated Number: 0.25  |  State: 0
[0][1] Generated Number: 0.79  |  State: 0
[1][0] Generated Number: 0.37  |  State: 0
[1][1] Generated Number: 0.97  |  State: 0

[0][0] Generated Number: 0.25  |  State: 0
[0][1] Generated Number: 0.38  |  State: 0
[1][0] Generated Number: 0.00  |  State: 0
[1][1] Generated Number: 0.25  |  State: 0

EDIT: This is my attempt for the minimal reproducible example. Below is the code I made to reproduce this:

Main.c

    #include "header.h"

int main() {
    srand((unsigned int) time(NULL));
    test();
    return 0;
}

Test.c

#include "header.h"

void test() {
    for (int i = 0; i < grid_w; ++i) {
        for (int j = 0; j < grid_h; ++j) {
            grid[i][j] = randomNumTest(i, j);
        }
    }
}

randomNumTest.c

#include "header.h"
    
    int randomNumTest(int x, int y) {
        float randNumber = (float) rand() / (float) (RAND_MAX) * (float) 1;
    
        printf("[%i][%i] Generated Number: %.2f\n", x, y, randNumber);
    
    }

Header.h

#include "stdio.h"
#include "stdlib.h"
#include "time.h"

#define grid_h 2
#define grid_w 2

void test();

int randomNumTest(int x, int y);

int grid[grid_h][grid_w];
Vapor
  • 1
  • 4
  • 2
    Call `srand((unsigned int) time(0));` one time total at the start of your `int main()`. Don't call it for each call of rand() – drescherjm Dec 07 '21 at 21:59
  • @drescherjm after calling that in my main it now outputs the first value as the same one upon each time running the code but the other 3 values as random, see here [0][0] Generated Number: 0.25 | State: 0 [0][1] Generated Number: 0.79 | State: 0 [1][0] Generated Number: 0.37 | State: 0 [1][1] Generated Number: 0.97 | State: 0 (first run) [0][0] Generated Number: 0.25 | State: 0 [0][1] Generated Number: 0.38 | State: 0 [1][0] Generated Number: 0.00 | State: 0 [1][1] Generated Number: 0.25 | State: 0 (second run) – Vapor Dec 07 '21 at 22:03
  • @drescherjm I have the newest version of mingw, I only installed it a couple days ago. – Vapor Dec 07 '21 at 22:08
  • @Vapor Do not post code/output in the comment. Post the actual [mcve] and the output in the question. – Eugene Sh. Dec 07 '21 at 22:08
  • @EugeneSh. Apologies, was trying to provide the two outputs given after using the suggestion above. – Vapor Dec 07 '21 at 22:09
  • I could not find a link about the mingw/ seed bug I had thought about so I deleted the comment. You probably should add a [mcve] that shows a small (less than 30 line program) that duplicates the result you see along with several example outputs. – drescherjm Dec 07 '21 at 22:10
  • Use the [edit] link to add extra info to the question if needed. – Eugene Sh. Dec 07 '21 at 22:10
  • @vapor All relevant information belongs in the question itself. – pjs Dec 07 '21 at 22:10
  • @pjs I have updated the OP. Thank you for that feedback – Vapor Dec 07 '21 at 22:14
  • *the first value not being random but instead being incremented very slowly by one* - what does it mean? How many times did you run it? Again, show [mcve]. – Eugene Sh. Dec 07 '21 at 22:16
  • @EugeneSh. hopefully the new edit will help, aplogies for any troubles I'm fairly new to using Stackoverflow. – Vapor Dec 07 '21 at 22:33
  • There is no `main()` function shown – Eugene Sh. Dec 07 '21 at 22:37
  • @EugeneSh. Aplogies, pasted the wrong thing in from my clipboard, updated. – Vapor Dec 07 '21 at 22:39
  • Looks OK so far (up to missing includes). Are you saying the first number is *always* `0.25`? How many times did you run it? – Eugene Sh. Dec 07 '21 at 22:40
  • @EugeneSh. So as far as I've tested, the first number would be the same for ~60 seconds (I haven't counted the time between it's changes it just seems as if a minute passes by each time) and then would increment by one while the others change constantly. I can run it as many times and it would stay the same, for example running it 5 times one after the other. – Vapor Dec 07 '21 at 22:46
  • Please show your *actual* code, including the `#include` lines. Are you getting any compiler warnings? Have you included `time.h`? `stdlib.h` ?? – Eugene Sh. Dec 07 '21 at 22:49
  • @EugeneSh. The `#include` lines are located in header.h to which each c file is linked to. No compiler warnings at all. Both `time.h` and `stdlib.h` are included in `header.h`. – Vapor Dec 07 '21 at 22:51
  • Sorry, can't reproduce it. https://ideone.com/kTcxp0 Anyway, few remarks - functions that don't take parameters should be defined as `int foo(void)`. The header file should not contain varible *definitions* such as `int grid[grid_h][grid_w];`, because it can be included several times in different sources (which is actually happening in your case - each `.c` file will have its own copy of it). – Eugene Sh. Dec 07 '21 at 22:58
  • @EugeneSh. Tried your entire code and the first value at [0][0] is still remaining the same and to confirm it does increment by one after each minute passes. This is very weird... It is currently at 60, in less than a minute from now it will turn into 61. – Vapor Dec 07 '21 at 23:02
  • I have reproduced this with mingw 11.2 on VSCode on Win10. – drescherjm Dec 07 '21 at 23:16
  • @drescherjm Ah, that what happens when using ideone to repro, and it doesn't show warnings.. – Eugene Sh. Dec 07 '21 at 23:21
  • @drescherjm in terms of finding a solution to fix this issue, what can be done, if any? – Vapor Dec 07 '21 at 23:21
  • This was the link I was originally thinking of however I don't think it applies for multiple reasons: [https://stackoverflow.com/questions/18880654/why-do-i-get-the-same-sequence-for-every-run-with-stdrandom-device-with-mingw](https://stackoverflow.com/questions/18880654/why-do-i-get-the-same-sequence-for-every-run-with-stdrandom-device-with-mingw) – drescherjm Dec 07 '21 at 23:24
  • ***in terms of finding a solution to fix this issue, what can be done, if any?*** An ugly workaround would be to throw away the first value from rand(). Not sure why the first value is always the same or increasing by 1. Voted to reopen because with the example I believe it's a different problem than the original. – drescherjm Dec 07 '21 at 23:26
  • @drescherjm: The C standard does not make it undefined behavior for a function with a non-void return type not to return a value. – Eric Postpischil Dec 07 '21 at 23:36
  • 1
    @drescherjm TL;DR: never seed with `time(0)`, and particularly if it's possible that you're using a linear congruential random number generators (LCG). You shouldn't seed any RNG with the time, except possibly the nanosecond part, because succesive seeds could be the same. With LCGs it's particularly important because it takes a few iterations before you get divergence between two sequences seeded with similar values, and two seeds whose values differ by a small integer are similar. – rici Dec 08 '21 at 01:01
  • Also note that the problem description is imprecise. It's not that "the random number is being incremented slowly by one." Dividing by `RAND_MAX` and then showing two decimal points means that you only see differences of at least `RAND_MAX/100`, which is significantly larger than 1. The returned random number changes every second, but not by enough to bump it over that threshold. – rici Dec 08 '21 at 01:11
  • I've noticed that instead of assigning the output of `rand()` in a variable, but putting it directly in a `printf` statement, the first digit at `[0][0]` is always random now. So my new question is, why if I use the variable `randNumber` does it remain the same and increment by one vs when it's in a `printf`? – Vapor Dec 08 '21 at 09:21

0 Answers0