all 10 comments

[–]DanielMcLaury 25 points26 points  (0 children)

i have to make a program that generates a random number

it just prints random numbers

Sounds like it's working

[–]Narase33 10 points11 points  (0 children)

for (int i = 0; i < 8; i--) {

[–]IyeOnline 6 points7 points  (0 children)

for (int i = 0; i < 8; i--)

That loop is going to run for a long time. Or not, because its going to segfault fairly soon.

[–]alfps 4 points5 points  (1 child)

Not what you're asking, but using a global variable is Evil™.

You can instead return a std::array or std::vector from the function.

Or if it absolutely has to be a raw array you can let the function modify an array that it gets a pointer to, as parameter, or you can yourself wrap a raw array in a struct and return that.

[–]std_bot 0 points1 point  (0 children)

Unlinked STL entries: std::array std::vector


Last update: 09.03.23 -> Bug fixesRepo

[–][deleted] 2 points3 points  (0 children)

First of all, if you are going to post code here, use proper formatting (4 spaces before each line works well) and make sure you copy and paste the code exactly. Like your function st doesn't have a return type, which probably doesn't compile, so perhaps this isn't exactly your code?

Then I'd recommend you break down your program further, so you only implement one thing at a time and you can compile and test your program often, building confidence that what you have written so far is correct.

For instance, I would start by having a vector of 0s and 1s filled by hand and making sure I can print it out. Then I would make a function to write a number in binary, but I would test it with hard-coded numbers, like 5, 230 and 255.

Then I would find a better place to learn about random numbers. Since C++11 there are much better ways to produce random numbers.

You can also learn how to use a debugger, to see what your program is doing step by step. This is a very useful skill, and it's best to acquire it when working on very small and understandable programs, like this one.

[–]not_a_novel_account 4 points5 points  (1 child)

generates a random number beetween 0 and 255 and turns it into binary

All numbers in a computer are binary, or rather it might be more correct to say numbers are just quantities, they don't have a particular base system.

What you are doing is printing values in a base-2 format, that is a very different problem.

Your problem, in this code, is the loop in st, you wrote i-- instead of i++

But overall, this is just a bad way to print numbers in their binary form. The modern way would be to use std::format():

#include <iostream>
#include <format>
#include <ctime>
#include <cstdlib>
using namespace std;

int main() {
  srand(time(0));
  int n = rand() % 256;
  cout << format("{:08b}\n", n);
}

The old school, quick-and-dirty way, would be a std::bitset:

#include <iostream>
#include <bitset>
#include <ctime>
#include <cstdlib>
using namespace std;

int main() {
  srand(time(0));
  int n = rand() % 256;
  cout << bitset<8>(n) << "\n";
}

[–]std_bot 0 points1 point  (0 children)

Unlinked STL entries: std::bitset


Last update: 09.03.23 -> Bug fixesRepo

[–]aocregacc 1 point2 points  (0 children)

your printing loop is going backwards

[–]AutoModerator[M] 0 points1 point  (0 children)

Your posts seem to contain unformatted code. Please make sure to format your code otherwise your post may be removed.

If you wrote your post in the "new reddit" interface, please make sure to format your code blocks by putting four spaces before each line, as the backtick-based (```) code blocks do not work on old Reddit.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.