Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

need a list

how do i make a list of numbers so that when ever i run a loop the loop will count through the list and display the number.

example:
(1,2,3,4) is the list
when the loop is done running it goes back to the list and if it generated a 5 then it would count through the numbers. so it would count 1-4 then go back to 1. with 6 it would count 1-4 then it would count one again and then it would display 2.

so how do i make a list of numbers.
[463 byte] By [tralfas] at [2007-11-11 7:38:49]
# 1 Re: need a list
can i do that with a string
like string one = {1,2,3,4}
tralfas at 2007-11-11 21:02:03 >
# 2 Re: need a list
find the modulus
walk to the value at the position of the modulus
nspils at 2007-11-11 21:03:08 >
# 3 Re: need a list
UH im a little confused. i didnt understand the last post and the code i tried didnt work. i got some stuff from the about website. the guy made a magic eight ball. so can anyone explain to me how i could make a random number generator. i want it to be like rolling dice. thats what the program is poorly trying to symbolize.

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

int main ()
{
string one[4] = {1,2,3,4};
int num;
cout << "press 1 for a d4" << endl
<< "press 2 for a d6" << endl
<< "press 3 for a d8" << endl
<< "press 4 for a d10" << endl
<< "press 5 for a d12" << endl
<< "press 6 for a d20" << endl
<< "press 7 for a d100" << endl;
cin >> num;
switch (num)
{
int j, n;
const int one = 4;
case 1: num = for (j=1; j< 100; j++){
rand();
}
srand( (unsigned)time( NULL ) );
n = j * rand() / (RAND_MAX + 1.0);
cout << string{j};
system ("pause");
return 0;
}
tralfas at 2007-11-11 21:04:07 >
# 4 Re: need a list
try using a do while loop with a nested if statement for when your count reaches the value 5
socram at 2007-11-11 21:05:02 >
# 5 Re: need a list
:confused: :confused: :confused: uhhhhhhh

do
{
rand()
}
while ( ?==5)
tralfas at 2007-11-11 21:06:12 >
# 6 Re: need a list
When I mentioned modulus, I was referring to calculating which index position you would be going to in your array of values with the number which was generated by your random number generator [if I am interpreting your code as meaning what you intended]:

int index = random number % 4; // if your array has four elements

now get the value at array[index]:

num = one[ index ];
nspils at 2007-11-11 21:07:12 >
# 7 Re: need a list
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;

int main()
{

string advice[5] = {
"Avram said so",
"Could be",
"Definitely",
"No Way",
"The line is drawn here"};

string disregardedQuestion;

int j;
const int N = 5;

srand( (unsigned)time( NULL ) );

//Increases randomness of advice
for (j = 0; j < 25; j++) {
rand();
}

cout << "Enter your question" << endl;
cin >> disregardedQuestion;

j = (int) N * rand() / (RAND_MAX + 1.0);

cout << advice[j] << endl;

return 0;
}
that is the code that i tried to adapt into this program. it can be found on this site : http://cplus.about.com/od/advancedtutorials/l/aa041303d.htm
tralfas at 2007-11-11 21:08:06 >
# 8 Re: need a list
how about

cout << advice[ j % 5] << endl;
nspils at 2007-11-11 21:09:14 >
# 9 Re: need a list
I don't see what you're trying to do with
j = (int) N * rand() / (RAND_MAX + 1.0);
or
for (j = 0; j < 25; j++) {
rand();
}
because that doesn't increase the randomness (as far as i know).. you're just generating a random number and doing nothing with it.

here's a simplified version of your code, with nspils correction.
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;

int main()
{

string advice[5] = {
"Avram said so",
"Could be",
"Definitely",
"No Way",
"The line is drawn here"
};

string disregardedQuestion;

srand((unsigned)time(NULL));

cout << "Enter your question" << endl;
cin >> disregardedQuestion;

cout << advice[rand() % 5] << endl;

return 0;
}

rand() will generate a random number according to the time, when you % 5, you're going to recieve a number 0 through 4.
destin at 2007-11-11 21:10:07 >
# 10 Re: need a list
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
srand( (unsigned)time( NULL ) );
int roll,num,times;

cout << "press 1 for a d4" << endl
<< "press 2 for a d6" << endl
<< "press 3 for a d8" << endl
<< "press 4 for a d10" << endl
<< "press 5 for a d12" << endl
<< "press 6 for a d20" << endl
<< "press 7 for a d100" << endl;
cin >> num;

cout << "enter the number of times you would like this to be rolled" << endl;
cin >> times;

switch (num){
case 1 : while (){
roll = 1 + (rand()%4);
}
}
system ("pause");
return 0;
}
ok new code i just need a loop that loops roll depending on how many times the user would like the dice rolled.
i did a for loop before but it kept displaying a wierd negative number. it was for (j=1; j<times; j++).idk im stuck.
tralfas at 2007-11-11 21:11:13 >
# 11 Re: need a list
Bleh. I don't know what d20, d100 or anything is :D. Is it the number of sides the die has? Here's a similar program:

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

int main()
{
srand((unsigned) time(NULL));
int roll, sides, times;

cout << "Enter how many sides the dice has: ";
cin >> sides;

cout << "Enter how many times this will be rolled: ";
cin >> times;

for (int i = 0; i < times; i++) {
roll = (rand() % sides) + 1;
cout << "You rolled a " << roll << endl;
}

return 0;
}
destin at 2007-11-11 21:12:12 >
# 12 Re: need a list
wow that works thanks alot. yeah d20 and d100 stand for the number of sides. its not a 100 sided dice you have to roll it twice or sometimes there are two sets of dice going from 1 - 10.
tralfas at 2007-11-11 21:13:16 >
# 13 Re: need a list
there actually are 100 sided dice but its about like rolling a golf ball (each pit is a number). 2 10 sided are preferred.
jonnin at 2007-11-11 21:14:12 >
# 14 Re: need a list
here is the final code
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
srand( (unsigned)time( NULL ) );
int roll,num,times;
char again;

do
{
cout << "press 3 for a d4" << endl
<< "press 4 for a d4" << endl
<< "press 6 for a d6" << endl
<< "press 8 for a d8" << endl
<< "press 10 for a d10" << endl
<< "press 12 for a d12" << endl
<< "press 20 for a d20" << endl
<< "press 100 for a d100" << endl;
cin >> num;
cout << "enter the number of times you would like this to be rolled" << endl;
cin >> times;

for (int j = 0; j < times; j++) {
roll = (rand() % num) + 1;
cout << "You rolled a " << roll << endl;
}
cout << "would you like to roll again?" << endl;
cin >> again;
}
while(again == 'y');
system ("pause");
return 0;
}
tralfas at 2007-11-11 21:15:18 >