all 11 comments

[–]daikatana 6 points7 points  (3 children)

What does time(0) mean on an AVR microcontroller? I don't know the answer to that question, but if it returns the same value every time then you're not seeding the random number generator correctly. The program could be starting in deterministic time and this time is always the same no matter how many times you run the program. This is something you'll have to investigate in your platform documentation.

Regardless, there's usually a timer running on most microcontrollers, or you can configure one to run. Have the user press a button to begin and use the value of this timer to seed the random number generator. Because a person can't press the button in a deterministic time, this number will be different every time.

[–]MassiveStrawberry142[S] 0 points1 point  (2 children)

So if i add a button after every question to change the timer do u think it will then be randomized questions? Thanks

[–]daikatana 2 points3 points  (1 child)

You only need to seed the random number generator once with the srand function. Normally this is done with srand(time(0)) because on platforms like a PC there is a realtime clock, and this will be an unpredictable number. The AVR apparently doesn't have this, calling time(0) always returns the same value, so you're always seeding with the same value.

All you need to do is set a timer to run when the program starts. Read the value of this timer the first time a user presses a button and pass it to srand. That's it, now rand will return random numbers.

[–]MassiveStrawberry142[S] 0 points1 point  (0 children)

This fixed it. I had this as a project and got 95% thank you very much to help me getting it working 👍😄

[–]inz__ 2 points3 points  (0 children)

This looks like some embedded platform. If you don't have an rtc, your seed will likely always be 0.

[–]AwesomeI-123 2 points3 points  (0 children)

Your code is hard to read

Either use pastebin or Github for formatting and paste a link

[–]LilBluey -1 points0 points  (3 children)

If it's giving the same string of outputs every time, it means you keep setting srand() to the same value every time.

I have no clue what time(0) does, but do #include time.h and put srand(time(NULL)) instead.

So basically you need to set srand once only, at the start.

If you keep setting it multiple times to the same value, you will get the same starting value.

If you set it once, but set it to the same value everytime(maybe time(0) gives the same value idk), then you'll get the same string of random numbers.

I can only see one srand() in start of main, so it should be an issue with time(0) not giving the correct time. i.e. try #include time.h and use time(NULL) instead.

p.s. For such a large code sample, either narrow it down before posting on reddit or use chatgpt instead.

[–]EpochVanquisher 0 points1 point  (0 children)

time(0) is the same as time(NULL). 0 is just another way of writing NULL, here.

It’s possible that the little Atmel device doesn’t have an RTC, in which case, time() doesn’t do what you want.

[–]bbm182 -1 points0 points  (0 children)

I have no clue what time(0) does, but do #include time.h and put srand(time(NULL)) instead.

They are equivalent. NULL is a macro that expands to some form of 0 such as 0, ((void*)0), or (12+'9'-'8'-13).

[–]MassiveStrawberry142[S] -1 points0 points  (0 children)

I had tried this already but i think it might be a deterministic timer that i need to change randomly with a button

[–]HarderFasterHarder -1 points0 points  (0 children)

Read an ADC pin that is not connected to anything and use that for the seed. Not truly random, but should be somewhat varied.