Click here to Skip to main content
14,920,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,I have two menus [menu1, menu2] I want to choose different names for these two menus according to the random number was generated(either 1 or 2), so, I used menu1[r1].c1[r1] and menu2[r2].c2[r2] to check if it is equal to zero in this case I choose name form menu1[r1].n1[r1] and menu2[r2].n2[r2].
So if all menu1[r1].c1[r1] =1 ,the program must generate different random number, the output must be mee1,mee2,me16,mee4,mee5,me13,mee7,mee8,mee9,me10,me11,me12,mee6,me14,me15,mee3

or in different arrange according to the random number that generates

What I have tried:

Updated 26-Feb-18 5:25am
phil.o 25-Feb-18 14:22pm
I'm actually trying, but I still have a very hard time understanding your requirement. What disturbs me the most is your usage of arrays as I feel that your are over-engineering your solution.
Please give me some time, I'll try to post a second solution trying to simplify things a bit.
Lilyanaa 25-Feb-18 14:38pm
thanks for your efforts, actually I don't know why the if conditions do not work, I just want the output consists of 16 names, eight from each menu and without duplicate in names
phil.o 25-Feb-18 14:45pm
OK, I see; actually you do not need the member variables in your structures being arrays (excepts for the names which are arrays of characters, bt I'll show you how to declare them). And you are using the member variable c to know whether a specified name already has been attributes, right?
Problem is: out of 16 random numbers 1 or 2, nothing guarantees that you will get 8 ones and 8 twos; you could have 10 ones and 6 twos, or 5 ones and 11 twos.
phil.o 25-Feb-18 14:43pm
So let me rephrase to be sure I understood:
you want in your final result an array of 16 f structures, each having a random distribution of "mee1", "mee2", ..., "me16" name, each name being unique and coming from menu1 or menu2 array depending on the random number?
Lilyanaa 25-Feb-18 15:04pm
Yes, so I use if(count1<=8) to guarantee that doesn't choose more than 8, if the new random number be 1 too, it must generate new random number and in the same time the .c1 be all = 1 because all the 8 names were chosen
phil.o 25-Feb-18 15:45pm
I understand, but you cannot do it like that. I am preparing a second answer which will present you with an alternative.
Lilyanaa 25-Feb-18 15:51pm
thank you so much, I'm waiting

To generate a random number either 1 or 2, you can use the following code:
#include <stdlib.h>

int random_number = (rand() % 2) + 1;

Hope this helps.
Lilyanaa 25-Feb-18 12:05pm
is the same, r = rand() % (2 + 1 - 1) + 1 ;
phil.o 25-Feb-18 12:27pm
Yes, it does. But, in this case, this produces exactly a random number which is either 1 or 2.
So, if that is not what you want, please try to rephrase your requirement.
Lilyanaa 25-Feb-18 12:39pm
I was explained what I want in my question, can u help, please?
phil.o 25-Feb-18 12:44pm
No, because I tried my best, but I cannot understand your requirement.
That's why I am asking you to rephrase it, because for now, I will be totally unable to help you.
Once I understand what it is you need, things should go much faster.
Lilyanaa 25-Feb-18 12:49pm
Ok, I will try, thanks
raddevus 25-Feb-18 12:53pm
I just compiled that and ran it about 10 times and I got 2 every time.
I put the program in a shell script and called it a number of times and still got 2 every time.
Does the rand() call need to be seeded first or something?
Just curious

Here's my code
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(){
int random_number = (rand() % 2) + 1;
cout << random_number <<endl;

I added the following line of code before the call to rand() and it seems to generate 1 as the output now too.
phil.o 25-Feb-18 13:46pm
Weird, because I did the same in VS and here what I got:
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
   for (int i = 0; i < 50; ++i) cout << (rand() % 2) + 1 << ' ';
   cout << '\n';

2 2 1 1 2 1 1 1 1 1 2 2 2 2 2 2 2 1 2 1 2 1 1 2 1 1 2 1 1 2 2 1 2 1 2 1 2 2 2 1 2 2 1 2 2 1 2 2 2 1
Press a key...
raddevus 25-Feb-18 16:39pm
That is interesting and could be because mine is the same program running multiple times --- meaning the random value is seeded the same every time it is run. I will add the loop to mine just to see what it does.

Mine is also compiled with g++ under Debian but since this is all standard library stuff that shouldn't matter.

Yep that's it. I added the for loop to call rand() ten times and I get pseudo-random list of values.
2 1 2 2 2 2 1 1 2 2
However, every time I run it I get that same list of 10 values, of course.

And, of course if I add the call to srand() then every time I run the program I get a more random list of 10 values.

Here's my final code:

#include <stdlib.h>
#include <iostream>
using namespace std;
int main(){
int random_number = (rand() % 2) + 1;
for (int i = 0; i< 10;i++){
cout << random_number << " " ;
random_number = (rand()%2)+1;
cout << endl;

k5054 26-Feb-18 13:20pm
You need to seed the random number generator, otherwise you always get the same sequence. This is done by calling srand(). I often use srand(time(0)) i.e. seed the random number generator with the current time, in seconds. That works under linux and probably mac-os too. Windows may do things differently.
I think I almost understood what you want to do, but I think you cannot do it the way you have coded (see my comment).
The problem is: if you take a random number (1 or 2) 16 times, nothing guarantees that you will have the same number of ones and twos. Even by setting a flag in the c member variable as you did, that completely messes up with the algorithm. I think having an array of 16 names that you would shuffle would be much cleaner.
You could do this way:
#include <time.h>

struct f
   const char* name;

// Shuffles an array
void shuffle(f menu[], size_t count);

// The size of the array
const int SIZE = 16;

int main() {
   // Declarations
   int i;
   struct f menu[SIZE];

   // Initializations
   menu[ 0].name = "mee1";
   menu[ 1].name = "mee2";
   menu[ 2].name = "mee3";
   menu[ 3].name = "mee4";
   menu[ 4].name = "mee5";
   menu[ 5].name = "mee6";
   menu[ 6].name = "mee7";
   menu[ 7].name = "mee8";
   menu[ 8].name = "mee9";
   menu[ 9].name = "me10";
   menu[10].name = "me11";
   menu[11].name = "me12";
   menu[12].name = "me13";
   menu[13].name = "me14";
   menu[14].name = "me15";
   menu[15].name = "me16";

   // Shuffle the numbers
   shuffle(menu, SIZE);

// Algorithm from
void shuffle(f menu[], size_t count) {
   size_t i, j;
   f t;
   srand((unsigned int)time(NULL));
   if (count > 1) {
      for (i = 0; i < count - 1; ++i) {
         j = i + rand() / (RAND_MAX / (count - i) + 1);
         t = menu[j];
         menu[j] = menu[i];
         menu[i] = t;

I changed your original algorithm a lot, but that is because I do not think you could achieve this the way you had done it. I kept the f structure, but got rid of str1 and str2 as they only introduced some unnecessary complexity.
I am aware this is a big change, and not knowing the exact requirement this may not be a valid solution. If it is not valid, my apologies. I don't think I would be able to only slightly modify your solution and achieve the same result.

As I said, I do not have a C compiler. I tested it on a VS 2017 C++ project and I actually get expected shuffling of names.
Lilyanaa 25-Feb-18 17:20pm
No, I must use two menus, logically my code must be correct if .c1 more than 8, must generate new random number, but I don't know why it doesn't work, anyway thank you very much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900