Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Dylan552

macrumors newbie
Original poster
Sep 1, 2010
11
0
Code:
if(calledNumbers.contains(recent))
{
    number = 1 + Bingo.nextInt(75);
}

so this is in java and it basically says if the random number has already been added then gernrate a new one. Also recent is the last number called placed in a string and callednumbers is a string of all the numbers. EX: "75 55 33 "So i was looking at the RangeofString in objective c and wnat to know how to do that in objective c . PLease help!

Thanks!!
 

wlh99

macrumors 6502
Feb 7, 2008
272
0
I wouldn't use any type of string to store a list of numbers that I have randomly picked already. I would use an array of numbers. A simple c array of int's would probably be easiest.

Also, rather than picking a random number, then checking if it has already been called, why not initialize the array with numbers 1-75, shuffle the array, then just call the numbers in order? Then you can eliminate the logic altogether.
 

Dylan552

macrumors newbie
Original poster
Sep 1, 2010
11
0
I wouldn't use any type of string to store a list of numbers that I have randomly picked already. I would use an array of numbers. A simple c array of int's would probably be easiest.

Also, rather than picking a random number, then checking if it has already been called, why not initialize the array with numbers 1-75, shuffle the array, then just call the numbers in order? Then you can eliminate the logic altogether.

i was going to use an array but didnt know how to do this could you explain thx
 

wlh99

macrumors 6502
Feb 7, 2008
272
0
i was going to use an array but didnt know how to do this could you explain thx

Unfortunately I don't have time to write a tutorial. Google is your friend, just search for "c array"

Code:
int numbers[74];
for (int i=0;i<75;i++) {
numbers[i]=i+1;
}
Would create an array of 75 elements, and fill it with the numbers 1 - 75.
 

RonC

macrumors regular
Oct 18, 2007
108
0
Chicago-area
Unfortunately I don't have time to write a tutorial. Google is your friend, just search for "c array"

Code:
int numbers[74];
for (int i=0;i<75;i++) {
numbers[i]=i+1;
}
Would create an array of 75 elements, and fill it with the numbers 1 - 75.

Not exactly, it would create a vector with 74 elements, fill it with numbers 1-74, and then the value 75 would be written to the memory address immediately after the end of the 74th element.

That will likely cause weird unpredictable stuff to happen. Not in a simple program like this one, but buried somewhere in even a modestly more complex program will make you pull your hair out. (Consider that a lesson from my past; I'm very sensitive to those things now.)

Ron C.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Not exactly, it would create a vector with 74 elements, fill it with numbers 1-74, and then the value 75 would be written to the memory address immediately after the end of the 74th element.

Code looks fine to me and should work as predicted. Why do you say otherwise?
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,420
A sea of green
Code looks fine to me and should work as predicted. Why do you say otherwise?

I think you missed something.

This declaration:
Code:
int numbers[74];
is an array of 74 ints. The range of valid subscripts is 0 thru 73, inclusive.

This loop:
Code:
for (int i=0;i<75;i++) {
numbers[i]=i+1;
}
will overflow the array when i exceeds 73. There is one integer that exceeds 73 (the max valid array index) and is less than 75 (the loop ending condition): 74. So when i is 74, the assignment in the loop body will write to a location past the end of the array.
 

Dylan552

macrumors newbie
Original poster
Sep 1, 2010
11
0
I think you missed something.

This declaration:
Code:
int numbers[74];
is an array of 74 ints. The range of valid subscripts is 0 thru 73, inclusive.

This loop:
Code:
for (int i=0;i<75;i++) {
numbers[i]=i+1;
}
will overflow the array when i exceeds 73. There is one integer that exceeds 73 (the max valid array index) and is less than 75 (the loop ending condition): 74. So when i is 74, the assignment in the loop body will write to a location past the end of the array.

so how whould i chec to c if a number has already been called?
 

RonC

macrumors regular
Oct 18, 2007
108
0
Chicago-area
Try CFMutableSet

so how whould i chec to c if a number has already been called?

You're on the right path. I'll assume that "calledNumbers" is some kind of container. That container has the "contains()" method defined on it.

The equivalent iOS container is likely CFMutableSet (a subclass of CFSet). Look it up (here) and see if it works for you. You can probably get away with using int's because it stores (and likely compares) pointers (as "void *").

I don't like that pointer comparison thing, since two objects that happen to be equal will not compare as equal when you compare pointers to these apparently equal values, but as long as you know going in it can be ok.

if you have a CFMutableSet bob and int newRandomValue, you can use CFSetContainsValue(bob, (void *)newRandomValue) to test if the element exists in the set, and you can add it into the set by using CFSetAddValue(bob,(void*)newRandomValue).

No guarantees on this; give it a spin and see what happens.

Ron
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.